How to create an array of empty lists?

Question:

I’m struggling a bit with NumPy’s build in arrays.

I have a list with values:

l = ["a", "b", "c", "d", "e"]

The len(l) of the list is 5 and I want to create an array with the following structure.

array = [[][][][][]]

The inner array amount should be the length of the list l. (5)

Is there any solution to this with NumPy or is there a solution doing this with the build in list from python.

Asked By: LordLazor

||

Answers:

Using python:

array = [[] for _ in range(len(l))]

Output: [[], [], [], [], []]

With numpy:

array = np.zeros(shape=(len(l), 0))

Output: array([], shape=(5, 0), dtype=float64) (this is the same as np.array([[], [], [], [], []]))

Answered By: mozway

With bulit in list:

n = 5
arr = [[] for x in range(n)]
arr
Answered By: Edo Wexler
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.