How to convert a NumPy tuple to a list in Python [3 Methods]

Do you want to convert a NumPy tuple into a list? In this NumPy article, I will explain how to convert a NumPy tuple to a list in Python using different methods with some illustrative examples.

To convert a NumPy tuple to a list in Python, you can use the list() constructor for a straightforward conversion of individual tuples, employ list comprehension for converting each element in an array of tuples, or utilize the np.tolist() function to transform an entire NumPy array of tuples into a nested list.

Convert a NumPy tuple to a list in Python

There are three different methods to convert a NumPy tuple to a list in Python:

  1. Using list() function
  2. Using list comprehension
  3. Using np.tolist() function

Let’s see them one by one in detail with some examples:

1. NumPy tuple to list using list() function

The list() function is used to create a list in Python. We can pass the NumPy tuple to the function, and it returns a new list containing all tuple elements.

For instance:

import numpy as np

statue_of_liberty_coords = np.array([(40.6892, -74.0445)])

coords_list = list(statue_of_liberty_coords)
print(coords_list)
print(type(coords_list))

Output:

[array([ 40.6892, -74.0445])]
<class 'list'>

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

convert a NumPy tuple to a list in Python

2. Convert tuple to NumPy array using list comprehension

List comprehension in Python is an efficient way to create lists in Python. It’s handy when dealing with an array of tuples. We iterate over each NumPy array tuple element and convert each into a list.

READ:  Modulenotfounderror no module named tensorflow Keras

For instance:

import numpy as np

landmarks_coords = np.array([(37.8199, -122.4783), (38.8977, -77.0365)])
landmarks_coords_list = [list(coords) for coords in landmarks_coords]
print(landmarks_coords_list)

Output:

[[37.8199, -122.4783], [38.8977, -77.0365]]

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

python convert tuple to numpy array

3. Python convert tuple to NumPy array using np.tolist() function

The tolist() method is a NumPy array method that converts the array into a nested list. If our NumPy array consists of tuples, tolist() will convert each tuple into a list, and then these lists are nested within an outer list.

This method is particularly useful when we have a multi-dimensional NumPy array and we want to convert the entire structure into a list format.

For instance:

import numpy as np

city_populations = np.array([("New York", 8419000), ("Los Angeles", 3971000), ("Chicago", 2716000)])
city_populations_list = city_populations.tolist()
print(city_populations_list)

Output:

[['New York', '8419000'], ['Los Angeles', '3971000'], ['Chicago', '2716000']]

The screenshot below visually represents the output from running the code in PyCharm.

numpy array to list of tuples in Python

Conclusion

Understanding all the methods to convert a NumPy tuple to a list in Python like list() function, list comprehension, and np.tolist() function with some illustrative examples.

Each method is effective, with the choice depending on whether we’re converting a single tuple, an array of tuples, or the entire array structure.

You may also like to read these tutorials: