0

Suppose I have 2D array, for simplicity, a=np.array([[1,2],[3,4]]). I want to convert it to list of arrays, so that the result would be:

b=[np.array([1,2]), np.array([3,4])]

I found out that there is np.ndarray.tolist() function, but it converts N-D array into nested list. I could have done in a for loop (using append method), but it is not efficient/elegant.

In my real example I am working with 2D arrays of approximately 10000 x 50 elements and I want list that contains 50 one dimensional arrays, each of the shape (10000,).

Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47

2 Answers2

5

How about using list:

a=np.array([[1,2],[3,4]])
b = list(a)
jh314
  • 27,144
  • 16
  • 62
  • 82
3

Why don't you use list comprehension as follows without using any append:

a=np.array([[1,2],[3,4]])
b = [i for i in a]
print (b)

Output

[array([1, 2]), array([3, 4])]
Sheldore
  • 37,862
  • 7
  • 57
  • 71