dinodeep
dinodeep

Reputation: 163

2D Numpy Array to List of Numpy Arrays

I have a 2D numpy array of dimensions of 1000 by 1000. When I look at the type, I get

>>>print(type(arr)) 
<class 'numpy.ndarray'>

>>>print(type(arr[0]))
<class 'numpy.ndarray'>

I would like to convert this 2D array into a list of numpy arrays, and I need this conversion to be quick so that it will work for arrays of larger scales. I want to convert it so that I get these results

>>>print(type(arr)) 
<class 'list'>

>>>print(type(arr[0]))
<class 'numpy.ndarray'>

Could anyone help me out with this? Thank you.

Upvotes: 2

Views: 619

Answers (1)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

Just use list(arr):

>>> arr = np.arange(12).reshape(3, 4)
>>> arr
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> list(arr)
[array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8,  9, 10, 11])]

>>> print(type(arr)) 
<class 'numpy.ndarray'>

>>> print(type(arr[0]))
<class 'numpy.ndarray'>
>>> arr = list(arr)

>>> print(type(arr)) 
<class 'list'>

>>> print(type(arr[0]))
<class 'numpy.ndarray'>

Upvotes: 3

Related Questions