How to convert NumPy Array to Python List?

tolist() function is used to convert Numpy array (ndarray) to list data structure. You use tolist() funtion with 1-Dimensional, 2-Dimensional, and multi-dimensional Numpy array. It does not modify the original array and returns a copy of array data in the form of a list.

Python program to convert Numpy array to list

import numpy as np

one = np.array([5, 10])
print(one.tolist())

two = np.array([[5, 10], [15, 20]])
print(two.tolist())

three = np.array([[[5, 10], [15, 20]], [[25, 30], [35, 40]]])
print(three.tolist())

Output of the above program

[5, 10]
[[5, 10], [15, 20]]
[[[5, 10], [15, 20]], [[25, 30], [35, 40]]]

Recommended Posts