Convert NumPy array to list in python

How to convert NumPy array to list in python ?

In this article we are going to discuss about how we can convert a 1D or 2D or 3D Numpy Array to a list or list of lists.

Converting Numpy Array to List :

In Python, ndarray class of Numpy Module provides tolist() function which can be used to convert a 1D array to list. All the elements of the 1D array will be contained as the items of the list.

So, let’s see how it actually works.

# Program :

import numpy as np
# NumPy array created
arr = np.array([1, 2, 3, 4, 5])
# Printing the NumPy array
print('Numpy Array:', arr)

# Converting 1D Numpy Array to list
num_list = arr.tolist()

# Printing the list
print('List: ', num_list)
Outptut :
Numpy Array: [1 2 3 4 5] 
List: [1, 2, 3, 4, 5]

Converting 2D Numpy array to list of lists :

We can use the same tolist() function to convert the 2D Numpy array to list of lists i.e called as Nested List.

So, let’s see how it actually works.

# Program :

import numpy as np
# 2D Numpy Array created
arr = np.array([[11, 12, 13, 14],
                [15, 16, 17, 18],
                [19, 20, 21, 22]])
# Printing 2D numpy array                
print('2D Numpy Array:')
print(arr)

# Converting Numpy Array to list of lists
list_of_lists = arr.tolist()

#Printing the nested list
print('List of lists:')
print(list_of_lists)
Output :
2D Numpy Array:
[[11, 12, 13, 14], 
[15, 16, 17, 18], 
[19, 20, 21, 22]]
List of lists:

Converting 2D Numpy Array to a flattened list :

We can also convert the 2D NumPy array to a flat list. For that first we have to convert the 2D NumPy array to 1D array by using flatten() function. Then convert 1D array to list by using tolist() function.

So, let’s see how it actually works.

# Program :

import numpy as np
# 2D Numpy Array created
arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8],
                [3, 3, 3, 3]])
# Printing 2D Numpy array                
print('2D Numpy Array:')
print(arr)

# Converting 2D Numpy array toa single list
num_list = arr.flatten().tolist()

#Printing list
print('List:', num_list)
Output :
2D Numpy Array:
[[1, 2, 3, 4], 
[5, 6, 7, 8], 
[3, 3, 3, 3]]
2D Numpy Array:
[1, 2, 3, 4, 5, 6, 7, 8, 3, 3, 3, 3]

Convert 3D Numpy array to nested list :

Similarly the 3D Numpy array can also be converted by using tolist() function into list of nested lists.

So, let’s see how it actually works.

# Program :

import numpy as np

# 3D Numpy Array created
arr = np.ones( (2,4,5) , dtype=np.int64)

#Prinitng 3D Numpy array 
print('3D Numpy Array:')
print(arr)

# Converting 3D Numpy Array to nested list
nested_list = arr.tolist()

# Printing nested list
print('Nested list:')
print(nested_list)
Output :
3D Numpy Array:
[[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]


[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]]
Nested list:
[[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]],
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1], 
[1, 1, 1, 1, 1]]]

Converting 3D Numpy Array to a flat list :

The process is same like how we converted the 2D Numpy array to flatten list. Similarly, use the flatten() function to convert the 3D Numpy array to 1D array. Then convert the 1D Numpy array to flat list by using tolist() function.

So, let’s see how it actually works.

# Program :

import numpy as np
# 3D Numpy Array created
arr = np.ones( (2,4,5) , dtype=np.int64)
# Printing 3D Numpy array
print('3D Numpy Array:')
print(arr)

# Converting 3D Numpy Array to flat list
flat_list = arr.flatten().tolist()

# Printing the list
print('Flat list:')
print(flat_list)
Output :
3D Numpy Array:
[[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]]
Flat list:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]