How to Convert NumPy Array to List of Lists in Python

Converting a NumPy array to a list of lists can be useful when we want to work with nested data structures or if we need to pass our data to functions or libraries that require a list of lists as input. In this tutorial, we will explore different methods of converting NumPy arrays to lists of lists in Python.

There are 4 ways to convert Python numpy array to list of lists, which are shown below.

  • Convert NumPy array to list of lists in Python using tolist() method
  • Convert NumPy array to list of lists in Python using list comprehension
  • Convert NumPy array to list of lists in Python loop
  • Convert NumPy array to list of lists in Python iteration

We will begin by introducing the tolist() method, which is the simplest and most straightforward way to convert a NumPy array to a list of lists in Python.

How to convert NumPy array to list of lists in Python

A list of lists is a nested list where each inner list represents a row of data, and each element in the inner list represents a column. So, there are several ways to convert a NumPy array to a list of lists in Python. Which are discussed below.

Method-1: Convert NumPy array to list of lists using tolist() method

You can use the tolist() method of NumPy to convert the numpy array into a list of lists in Python.

# Importing numpy library
import numpy as np

# Create a numpy array using np.array() method
array = np.array([['United Kingdom'],['USA'],['Brazil']])

# Convert numpy array to list of list using the tolist() method
lst_of_lst = array.tolist()

# Print the list of list
print(lst_of_lst)

The above code first creates a NumPy array with three rows and one column.

Then we use the tolsit() method to convert the array to a list, which we store in the variable lst_of_lst. Finally, we print the resulting list of lists to the console.

How to convert numpy array to list of lists in Python using the tolist
Convert numpy array to list of lists in Python using the tolist() method

This is how to use the tolist() method to convert a NumPy array to list of lists in Python.

Method-2: Convert NumPy array to list of lists using list comprehension

You can use Python list comprehension to iterate over the rows of the NumPy array and convert each row to a list.

# Importing numpy library
import numpy as np

# Create a numpy array using np.array() method
array = np.array([['Canada'],['USA'],['Brazil']])

# Convert the NumPy array to a list of lists using a list comprehension
# Iterate through the each row in the Numpy array, convert it to list
lst_of_lst =[list(row) for row in array]

# Print the list of list
print(lst_of_lst)

The above code demonstrates how to convert a NumPy array to a list of lists using list comprehension.

  • First, importing the numpy library. Next numpy array is created using the np.array() method. This array contains the three string values.
  • Then, list comprehension is used to convert the NumPy array to a list of lists. In this case, each row of the NumPy array is converted to a list using the list() method.
  • Then all these lists are combined into a single list using the square brackets notation []. The resulting list of lists is stored in the variable lst_of_lst.
  • Finally, the print() function is used to display the resulting list of lists on the console.
Output: [['Canada'], ['USA'], ['Brazil']]

Method-3: Convert NumPy array to list of lists using loop

You can also convert a NumPy array to a list of lists using a loop in Python.

Here is an example.

# Import the NumPy libarary
import numpy as np

# Create a numpy array
array = np.array([['United Kingdom'],['USA'],['Brazil']])

# Conver the Numpy array to list of lists using a loop
lst_of_lst = []

# Iterate through each row in the Numpy array
for i in range(len(array)):
  row = []

#Iterate through each element in the current row
  for j in range(len(array[i])):


# Append the element to the current row
    row.append(array[i][j])

# Append the current row to the list of lists
  lst_of_lst.append(row)

# Prin the list of list
print(lst_of_lst)

The above code shows how to convert the numpy array to a list of lists using a Python nested loop.

  • The outer loop iterates through each row in the NumPy array, while the inner loop iterates through each element in the current row.
  • For each element, it is appended to the current row, and the current row is appended to the list of lists after all the elements in the row have been processed.
  • The resulting list of lists is printed as output.
How to convert numpy array to list of lists in Python using loop
How to convert numpy array to list of lists in Python using loop

Method-4: Convert NumPy array to list of lists using iteration

Converting a NumPy array to a list of lists using iteration is another way to achieve this.

# Importing NumPy library
import numpy as np

# Creating numpy array
array = np.array([['United Kingdom'],['USA'],['Brazil']])

# Convert NumPy array to list of lists using iteration
lst_of_lst = [[element for element in row] for row in array]

# Print the list of list
print(lst_of_lst)

The above code creates a numpy array with three values. Then use a nested list comprehension to convert the NumPy array to a list of lists.

  • The outer loop iterates over each row of the NumPy array. For each row, the inner loop iterates over each element in the row and appends it to the new list. This new list is then appended to the lst_of_lst list.
  • After iterating over all the rows of the NumPy array, Finally, the list of lists is printed to the console.
Output: [['United Kingdom'], ['USA'], ['Brazil']]

Conclusion

Converting a NumPy array to a list of lists in Python can be a useful technique in many data analysis and machine learning applications. By using the tolist() method, we can easily and efficiently convert a NumPy array to a nested list structure in Python. We can also use other methods like loop or list comprehension to achieve the same goal with more customization.

In this Python tutorial, we have covered 4 methods to convert the numpy array to list of lists in Python with different examples.

  • How to Convert numpy array to list of lists in Python using tolist() method
  • How to Convert numpy array to list of lists in Python using list comprehension
  • How to Convert numpy array to list of lists in Python loop
  • How to Convert numpy array to list of lists in Python iteration

You may also like: