How to Convert NumPy Array to List in Python (With Examples)


You can use the following basic syntax to convert a NumPy array to a list in Python:

my_list = my_array.tolist()

The following examples show how to use this syntax in practice.

Example 1: Convert 1-Dimensional Array to List

The following code shows how to convert a 1-dimensional NumPy array to a list in Python:

import numpy as np

#create NumPy array
my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  
#convert NumPy array to list                
my_list = my_array.tolist()

#view list
print(my_list)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#view object type
type(my_list)

list

Example 2: Convert Multi-Dimensional Array to List

The following code shows how to convert a multi-dimensional NumPy array to a list in Python:

import numpy as np

#create NumPy array
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
  
#convert NumPy array to list                
my_list = my_array.tolist()

#view list
print(my_list)

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]

#view object type
type(my_list)

list

Example 3: Convert Multi-Dimensional Array to Flattened List

The following code shows how to convert a multi-dimensional NumPy array to a flattened list in Python:

import numpy as np

#create NumPy array
my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
  
#convert NumPy array to flattened list                
my_list = my_array.flatten().tolist()

#view list
print(my_list)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#view object type
type(my_list)

list

Additional Resources

The following tutorials explain how to perform other common conversions in Python:

How to Convert List to NumPy Array
How to Convert Pandas Series to NumPy Array
Convert Pandas DataFrame to NumPy Array

Leave a Reply

Your email address will not be published.