Convert Numpy Array to List

NumPy – Convert Array to a Python List

To convert a Numpy Array into List in Python, call numpy.tolist() function and pass the numpy array as argument to this function.

The syntax to call numpy.tolist() to convert a numpy array into list is

numpy.tolist(arr)

where arr is a numpy array.

Return Value

numpy.tolist() returns an object of type list.

Examples

1. Convert 1D numpy array to a list

In this example, we take a numpy array in arr, and convert this numpy array into list using numpy.tolist().

Python Program

import numpy

arr = numpy.array([1, 2, 3, 4, 5, 10, 15])
x = arr.tolist()

print("Numpy Array :\n", arr)
print("\nList :\n", x)
Run Code Copy

Output

Numpy Array :
 [ 1  2  3  4  5 10 15]

List :
 [1, 2, 3, 4, 5, 10, 15]

2. Convert 2D numpy array to a list

If the numpy array has more than one-dimension, then the returned list is a nested list.

In the following program, we take a 2D numpy array in arr, and convert this numpy array into a list using numpy.tolist().

Python Program

import numpy

arr = numpy.array([[1, 2], [3, 4]])
x = arr.tolist()

print("Numpy Array :\n", arr)
print("\nList :\n", x)
Run Code Copy

Output

Numpy Array :
 [[1 2]
 [3 4]]

List :
[[1, 2], [3, 4]]

Summary

In this Numpy Tutorial, we learned how to convert a list into a numpy array, with the help of example program.

Related Tutorials

Code copied to clipboard successfully 👍