w3resource

NumPy Input and Output: ndarray.tolist() function

numpy.ndarray.tolist() function

The ndarray.tolist() function is used to get the array as a (possibly nested) list.

Syntax:

ndarray.tolist()

Version: 1.15.0

Returns: y : list
The possibly nested list of array elements.

Notes:
The array may be recreated, a = np.array(a.tolist()).

NumPy.ndarray.tolist() method Example-1:

>>> import numpy as np
>>> x = np.array([2, 4])
>>> x.tolist()

Output:

[2, 4]

NumPy.ndarray.tolist() method Example-2:

>>> import numpy as np
>>> x = np.array([[2, 4], [6, 8]])
>>> list(x)

Output:

[array([2, 4]), array([6, 8])]

NumPy.ndarray.tolist() method Example-3:

>>> import numpy as np
>>> x = np.array([[2, 4], [6, 8]])
>>> list(x)
>>> x.tolist()

Output:

[[2, 4], [6, 8]]

Python - NumPy Code Editor:

Previous: ndarray.tofile() function
Next: fromfile() function



Follow us on Facebook and Twitter for latest update.