Converting a 2D NumPy array into a list is a common requirement in data manipulation tasks. Typically, you have a two-dimensional array, possibly representing a matrix of numbers, and you want to convert it to a list of lists. Each sublist would correspond to a row in the original array. For example, given an input numpy
array such as [[1, 2], [3, 4]]
, the desired output is a list [[1, 2], [3, 4]]
.
Method 1: Using the tolist() Method
The tolist()
method is a built-in NumPy function designed to convert a NumPy array into a regular Python list. It preserves the nested structure of 2D arrays, resulting in a list of lists.
Here’s an example:
import numpy as np arr = np.array([[1, 2], [3, 4]]) list_of_lists = arr.tolist() print(list_of_lists)
Output:
[[1, 2], [3, 4]]
This code snippet creates a 2D NumPy array and uses the tolist()
method to convert it into a list of lists, which is then printed to the console.
Method 2: List Comprehension
List comprehension in Python is an elegant way to apply an operation to each element of a sequence. When working with a 2D NumPy array, a nested list comprehension can iterate over each row to create the nested list structure.
Here’s an example:
import numpy as np arr = np.array([[5, 6], [7, 8]]) list_of_lists = [list(row) for row in arr] print(list_of_lists)
Output:
[[5, 6], [7, 8]]
By using a nested list comprehension, this code converts each row of the NumPy array into a list, resulting in a list of lists.
Method 3: Using the map() Function
The map()
function applies a given function to every item of an iterable. When used with NumPy arrays, it can apply the list
function to each row to achieve the conversion to a list of lists.
Here’s an example:
import numpy as np arr = np.array([[9, 10], [11, 12]]) list_of_lists = list(map(list, arr)) print(list_of_lists)
Output:
[[9, 10], [11, 12]]
This snippet maps each row of the NumPy array to a list, resulting in an iterable of lists, which is then itself converted into a list.
Method 4: Using a For Loop
A straightforward for loop can be used to iterate over the rows of a 2D NumPy array, appending each converted row as a list to a new list variable.
Here’s an example:
import numpy as np arr = np.array([[13, 14], [15, 16]]) list_of_lists = [] for row in arr: list_of_lists.append(list(row)) print(list_of_lists)
Output:
[[13, 14], [15, 16]]
This code iterates over the array rows, converting each one into a list and appending it to the list_of_lists, which becomes our final list of lists result.
Bonus One-Liner Method 5: Using numpy.ndarray.flat
If you’re content with a flat list instead of a list of lists, you can use the flat
attribute of a NumPy array to return a flattened iterator, which can be readily converted into a single flattened list.
Here’s an example:
import numpy as np arr = np.array([[17, 18], [19, 20]]) flat_list = list(arr.flat) print(flat_list)
Output:
[17, 18, 19, 20]
This one-liner uses the flat
attribute to create an iterator of all array elements and then converts it to a flat list, discarding the original nested structure.
Summary/Discussion
- Method 1: tolist() Method. The simplest and most straightforward approach. Requires no imports other than NumPy. However, may not be as flexible if you need to transform data during conversion.
- Method 2: List Comprehension. Pythonic and concise. Good for readability and can include conditional logic if needed. Slightly less performant than the
tolist()
method on large arrays. - Method 3: map() Function. Functional approach, concise, and can be more efficient than list comprehension on large data sets. Less readable for those unfamiliar with functional programming concepts.
- Method 4: For Loop. Most explicit and easy to understand for beginners. It is more verbose and potentially less efficient than other methods.
- Method 5: Flat Attribute. Provides a one-liner to create a flat list. Loses nested list structure, so it serves a different purpose than the other methods which preserve it.