How to convert NumPy array to list ?
Last Updated :
01 Dec, 2023
This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding.
Convert NumPy Array to List
There are various ways to convert NumPy Array to List here we are discussing some generally used methods to convert NumPy array to list:
Convert NumPy Array to List using Type Casting
Here we are creating a Numpy array using the np.array and printing the array before the conversion and after the conversion using Python typecasting to list using list() function.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 4 , 5 ])
print ( "Before conversion: " , arr)
print ( type (arr))
arr = list (arr)
print ( "\nAfter conversion: " , type (arr))
print (arr)
|
Output:
Before conversion: [1 2 4 5]
<class 'numpy.ndarray'>
After conversion: <class 'list'>
[1, 2, 4, 5]
Convert NumPy Array to List using tolist() Method
Example 1: With One Dimensional Array
Here code uses NumPy to create an array, prints the array and its type, converts the array to a Python list using the `tolist()` method, and prints the resulting list along with its type.
Python3
import numpy as np
print ( "\nArray:" )
arr = np.array([ 1 , 2 , 4 , 5 ])
print (arr)
print ( type (arr))
lis = arr.tolist()
print ( "\nList:" )
print (lis)
print ( type (lis))
|
Output:
Array:
[1 2 4 5]
<class 'numpy.ndarray'>
List:
[1, 2, 4, 5]
<class 'list'>
Example 2: With Multidimensional Array
Here The code uses NumPy to create a 2D array, prints the array and its type, converts the 2D array to a nested Python list using the `tolist()` method, and prints the resulting list along with its type.
Python3
import numpy as np
print ( "\nArray:" )
arr = np.array([[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]])
print (arr)
print ( type (arr))
lis = arr.tolist()
print ( "\nList:" )
print (lis)
print ( type (lis))
|
Output:
Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
<class 'numpy.ndarray'>
List:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
<class 'list'>
Convert NumPy Array to List using list() Constructor
Here the code utilizes NumPy to create an array, then employs the list()
constructor to convert the array to a Python list. It subsequently prints both the NumPy array and the resulting list, along with their respective types, demonstrating the conversion process.
Python3
import numpy as np
num_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = list (num_array)
print ( "NumPy Array:" )
print (num_array)
print ( "Type of NumPy Array:" , type (num_array))
print ( "\nList from NumPy Array:" )
print (list_from_array)
print ( "Type of List:" , type (list_from_array))
|
Output :
NumPy Array:
[1 2 3 4 5]
Type of NumPy Array: <class 'numpy.ndarray'>
List from NumPy Array:
[1, 2, 3, 4, 5]
Type of List: <class 'list'>
Convert NumPy Array to List using list Comprehension
Here the code utilizes NumPy to create an array, then employs list comprehension to convert the array into a Python list, and finally prints both the original NumPy array and the converted list.
Python3
import numpy as np
numpy_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = [element for element in numpy_array]
print ( "NumPy Array:" , numpy_array)
print ( "List from NumPy Array:" , list_from_array)
|
Output :
NumPy Array: [1 2 3 4 5]
List from NumPy Array: [1, 2, 3, 4, 5]
Convert NumPy Array to List using append() Method
Here the code uses NumPy to create an array, then converts it to a Python list by iterating through its elements and appending them using append() method to an initially empty list. The original array and the resulting list are printed for verification.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = []
for element in arr:
list_from_array.append(element)
print ( "Original NumPy Array:" , arr)
print ( "List Converted from NumPy Array:" , list_from_array)
|
Output :
Original NumPy Array: [1 2 3 4 5]
List Converted from NumPy Array: [1, 2, 3, 4, 5]
Conclusion
In conclusion, the process of converting a NumPy array to a list provides flexibility and compatibility within Python programming. By utilizing methods such as `tolist()` or employing iterative techniques like appending elements, developers can seamlessly transition between these two data structures. This versatility is particularly valuable in scenarios where the distinct functionalities of lists are required, allowing for efficient data manipulation and integration into various Python applications.