How to convert python list and numpy.ndarray to each other

How to convert?

To convert a python list to numpy.ndarray, we can use numpy.array() function, to convert numpy.ndarray to python list, we should use numpy.ndarray.tolist() function.

Here is an example:

>>> import numpy as np
>>> data = [[1,2,3],[4,5,6]]
>>> type(data)
<type 'list'>
>>> data_array = np.array(data) # convert list to numpy.ndarray
>>> type(data_array)
<type 'numpy.ndarray'>
>>> data_list = data_array.tolist() # convert numpy.ndarray to list
>>> type(data_list)
<type 'list'>

, , ,