The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars.
Data items are converted to the nearest compatible built-in Python type.
Example
import numpy as np
gfg = np.array([ 1 , 2 , 3 , 4 , 5 ])
print (gfg.tolist())
|
[1, 2, 3, 4, 5]
Syntax
Syntax: ndarray.tolist()
Parameters: none
Returns: The possibly nested list of array elements.
How to Convert NumPy Array to Python List
To convert a NumPy array to a Python list we use ndarray.tolist method of NumPy library in Python. It returns a copy of the array data as a Python list (Nested list in case of a multi-dimensional array).
Let us understand it better with an example:
Example:
In this example, we can see that by using ndarray.tolist() method, we can have the list of data items in ndarray.
# import the important module in python import numpy as np
# make an array with numpy gfg = np.array([[ 1 , 2 , 3 , 4 , 5 ],
[ 6 , 5 , 4 , 3 , 2 ]])
# applying ndarray.tolist() method print (gfg.tolist())
|
[[1, 2, 3, 4, 5], [6, 5, 4, 3, 2]]