2

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.

1 Answer 1

3

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'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.