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

share|improve this question
up vote 156 down vote accepted

Use tolist():

import numpy as np
>>> np.array([[1,2,3],[4,5,6]]).tolist()
[[1, 2, 3], [4, 5, 6]]
share|improve this answer

NumPy arrays have a tolist method:

In [1]: arr=np.array([[1,2,3],[4,5,6]])

In [2]: arr.tolist()
Out[2]: [[1, 2, 3], [4, 5, 6]]
share|improve this answer

You can also use the list keyword of Python to create a list from the numpy array:

import numpy as np
nda = np.arange(10).reshape(2,5)
ndlist = list(nda)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.