NumPy tolist()

The tolist() method converts a NumPy array to a Python list without changing its data or dimensions.

Here, the elements and dimensions of list1 are the same as those of array1.

Note: For a single-dimensional array, array1.tolist() is the same as list(array1).


tolist() Syntax

The syntax of tolist() is:

ndarray.tolist()

tolist() Arguments

The tolist() method takes no arguments.


tolist() Return Value

The tolist() method returns a Python list.


Example 1: Convert a Multidimensional Array

The tolist() method converts a multidimensional array into a nested list.

Output

[[[0, 1], [2, 3]], [[4, 5], [6, 7]]]

Difference Between tolist() and list()

The key differences between tolist() and list() are

  • The tolist() method converts a multidimensional array into a nested list whereas list() converts it to a list of arrays. For example,

Output

Using array.tolist():  [[1, 2], [3, 4]]
Using list(array):  [array([1, 2]), array([3, 4])]
  • tolist() changes NumPy data types to Python data types, but list() doesn't.

Output

Datatype of original array: <class 'numpy.int64'>
Datatype after using array.tolist(): <class 'int'>
Datatype after using array.tolist(): <class 'numpy.int64'>
  • tolist() works with 0d numpy arrays, but list() doesn't.

Output

123
TypeError: iteration over a 0-d array