In this post, we are going to understand how to Convert NumPy array to Python list that includes one-Dimensional, Two-Dimensional, Three-Dimensional lists of lists and flat lists with code examples.We are going to use tolist() function and loop to achieve this.
1. Convert one-Dimensional NumPy array to Python List
In this example, we are converting a one-Dimensional NumPy array to a list by using the tolist() function. The tolist() function does not take any argument. It converts the array to List and finally, we can confirm this by printing the result.
#Program to Convert NumPy array to Python List
import numpy as np
np_array = np.array([11,12,14,56,16])
np_array_to_list = np_array.tolist()
print('Numpy 1D array to List :',np_array_to_list )
print("Shape: ", np_array.shape)
print("datatype :",type(np_array_to_list))
Output
Numpy 1D array to List : [11, 12, 14, 56, 16]
Shape: (5,)
datatype : <class 'list'>
2. Convert 2D NumPy array to lists of list using tolist()
In this example, we are converting a 2D NumPy array to lists of list by using the function tolist()
#Program to Convert NumPy array to Python List
import numpy as np
np_array = np.array([[11,12,14],[21,22,23],[31,32,33]])
print('NumPy 2D original array :',np_array)
np_array_to_list = np_array.tolist()
print('\n Numpy 2D array to Lists of list :',np_array_to_list )
print("Shape: ", np_array.shape)
print("datatype :",type(np_array_to_list))
Output
NumPy 2D original array : [[11 12 14]
[21 22 23]
[31 32 33]]
Numpy 2D array to Lists of list : [[11, 12, 14], [21, 22, 23], [31, 32, 33]]
Shape: (3, 3)
datatype : <class 'list'>
3.Convert 2D NumPy array to lists of list using loop
In this example, we are converting NumPy 2D array using for loop to list of lists. We are iterating each row of the NumPy array converting each row into a list and appending it to an empty list (list_of_lists) using the tolist() function and then Finally printing the result.
#Program to Convert NumPy array to Python List
import numpy as np
np_array = np.array([[11,12,14],[21,22,23],[31,32,33]])
list_of_lists = list()
for row in np_array:
list_of_lists.append(row.tolist())
print('NumPy 2D original array :',np_array)
print('\n Numpy 2D array to Lists of list :',list_of_lists )
print("Shape: ", np_array.shape)
print("datatype :",type(list_of_lists))
Output
NumPy 2D original array : [[11 12 14]
[21 22 23]
[31 32 33]]
Numpy 2D array to Lists of list : [[11, 12, 14], [21, 22, 23], [31, 32, 33]]
Shape: (3, 3)
datatype : <class 'list'>
4. NumPy three-Dimensional array to Lists of list
In this example, We are creating an array of shapes (2,3,3) using the reshape() function and using tolist() function to convert a NumPy three-Dimensional array to a list.
#Program to Convert NumPy array to Python List
import numpy as np
np_array = np.arange(18).reshape((2, 3, 3))
print('NumPy 3D original array :',np_array)
np_array_to_list = np_array.tolist()
print('\n Numpy 3D array to List :\n',np_array_to_list )
print("Shape: ", np_array.shape)
print("datatype :",type(np_array_to_list))
Output
NumPy 2D original array : [[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]]
Numpy 2D array to List :
[[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]]]
Shape: (2, 3, 3)
datatype : <class 'list'>
5. Convert NumPy multidimensional array to a flat list
All the methods we have mentioned above were used to convert the NumPy array to a list of lists, what if we have to convert a multi-dimensional array to a flat list. In this example, we are using the flatten() method to convert a NumPy multi-dimensional Array to a flat list.
#Program to Convert NumPy array to Python List
import numpy as np
np_array = np.array([[11,12,14],[21,22,23],[31,32,33]])
print('NumPy 2D original array :',np_array)
np_array_to_list = np_array.flatten().tolist()
print('\n Numpy 2D array to flat List :',np_array_to_list )
print("Shape: ", np_array.shape)
print("datatype :",type(np_array_to_list))
Output
NumPy 2D original array : [[11 12 14]
[21 22 23]
[31 32 33]]
Numpy 2D array to flat List : [11, 12, 14, 21, 22, 23, 31, 32, 33]
Shape: (3, 3)
datatype : <class 'list'>
6. Convert NumPy Array to flat list Using itertools
In this example, we are converting a NumPy muti-dimensional Array to a flat list Using itertools.
#Program to Convert NumPy array to Python List
import numpy as np
from itertools import chain
lists_of_list = [[14,56,16], [21,78,25], [31,98,33]]
np_array = np.array(lists_of_list)
print(' original Numpy 2D array:',np_array )
np_array_to_list = list(chain.from_iterable(np_array))
print('\n Numpy 2D array to flat list:\n',np_array_to_list )
print("Shape: ", np_array.shape)
print("datatype :",type(np_array_to_list))
Output
original Numpy 2D array: [[14 56 16]
[21 78 25]
[31 98 33]]
Numpy 2D array to flat list:
[14, 56, 16, 21, 78, 25, 31, 98, 33]
Shape: (3, 3)
datatype : <class 'list'>
Conclusion
We have Explored different ways to Program to Convert NumPy array to Python List. which includes one-Dimensional, Two-Dimensional, Three-Dimensional lists of lists, and flat lists. We can use any of them as per our requirements.