Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

How do I convert a NumPy array to a Python List (for example [[1,2,3],[4,5,6]] ), and do it reasonably fast?

1 Answer

0 votes
by (106k points)
edited by

You can use tolist() method for converting NumPy array into Python List structure:-

import numpy as np

np.array([[1,2,3],[4,5,6]]).tolist()

image

One important point to note that this method converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the "nearest compatible Python type".  So, if you wish to preserve the numpy data types, you could call list() on your array instead, and you'll end up with a list of numpy scalars.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...