π‘ Problem Formulation: Python developers often use NumPy arrays for high-performance numeric computation. However, there are scenarios where a standard Python list is required for certain functionalities not supported by NumPy. This article demonstrates how to convert a NumPy array to a native Python list. Imagine you have a NumPy array np.array([1, 2, 3]) and you want to convert it to [1, 2, 3].
Method 1: Using tolist()
NumPy array objects have a method called tolist(), which returns the array as a nested Python list. Nested lists are created corresponding to the original array’s dimensionality. This method is straightforward and recommended for converting NumPy arrays to native Python lists.
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
Here’s an example:
import numpy as np numpy_array = np.array([[1, 2, 3], [4, 5, 6]]) normal_list = numpy_array.tolist() print(normal_list)
Output:
[[1, 2, 3], [4, 5, 6]]
This code snippet converts a 2D NumPy array into a nested Python list using the tolist() method, maintaining the same nested structure.
Method 2: List Comprehension
List comprehension is a concise syntax in Python for creating lists. It can be used to iterate over a NumPy array and create a list by extracting each item. This method offers more control over the conversion process, allowing for element-wise operations if needed.
Here’s an example:
import numpy as np numpy_array = np.array([[1, 2, 3], [4, 5, 6]]) normal_list = [elem for elem in numpy_array] print(normal_list)
Output:
[array([1, 2, 3]), array([4, 5, 6])]
In the example above, we created a Python list of NumPy arrays using list comprehension. To get a list of lists, modify the comprehension to [elem.tolist() for elem in numpy_array].
Method 3: Iterating with for loop
Another way to convert a NumPy array to a Python list is by using a simple for loop to iterate through the array and append each element to a new list. This traditional approach is easy to understand and implement, especially for those new to Python.
Here’s an example:
import numpy as np
numpy_array = np.array([1, 2, 3])
normal_list = []
for item in numpy_array:
normal_list.append(item)
print(normal_list)Output:
[1, 2, 3]
This snippet shows how to manually iterate over a NumPy array and append each element to a new list, resulting in a normal Python list.
Method 4: Using np.ndarray.flat
The np.ndarray.flat attribute returns an iterator over a NumPy array that can be used to construct a Python list. This method is useful for flattening a multi-dimensional array and then converting it into a list.
Here’s an example:
import numpy as np numpy_array = np.array([[1, 2, 3], [4, 5, 6]]) normal_list = list(numpy_array.flat) print(normal_list)
Output:
[1, 2, 3, 4, 5, 6]
Using np.ndarray.flat, we can iterate through a 2D NumPy array in a flat manner and convert it directly to a Python list.
Bonus One-Liner Method 5: Using np.ndarray.flatten() with a List Constructor
A combination of the NumPy flatten() method, which flattens a multi-dimensional array into a one-dimensional array, along with the Python list constructor, can swiftly create a normal list in one line.
Here’s an example:
import numpy as np numpy_array = np.array([[1, 2, 3], [4, 5, 6]]) normal_list = list(numpy_array.flatten()) print(normal_list)
Output:
[1, 2, 3, 4, 5, 6]
This example demonstrates using flatten() to convert a 2D NumPy array into a flat, one-dimensional array, followed by list constructor to convert it to a Python list.
Summary/Discussion
- Method 1:
tolist(). Simple and direct. Preserves multi-dimensional structure. Inefficient for very large arrays. - Method 2: List Comprehension. Customizable and Pythonic. Less straightforward for multi-dimensional arrays.
- Method 3:
forloop. Beginner-friendly. Verbose and potentially slow for large arrays. - Method 4:
np.ndarray.flat. Good for flattening arrays. Requires additional steps for multi-dimensional arrays. - Method 5:
flatten()with List Constructor. Clean one-liner. Not suitable if the original multi-dimensional structure is what you need.
