• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing How to Convert NumPy Array to List

How to convert a NumPy array to a list in Python? To convert a NumPy array (ndarray) to a Python list use ndarray.tolist() function, this doesn’t take any parameters and returns a Python list for an array. While converting to a list, it converts the items to the nearest compatible built-in Python type.

Advertisements

If the array is one-dimensional, a list with the array elements is returned (list of objects). For a multi-dimensional array, a nested list is returned (list of list of objects). In this article, I will explain how to convert an ndarray array to a list using the tolist() method with examples.

Note that in Python NumPy, ndarray is a multidimensional, homogeneous array of fixed-size items of the same type. You can create an ndarray object by using NumPy.array().

1. Quick Examples of Convert Array to List

If you are in a hurry, below are some quick examples of how to convert an array to a list.


# Quick examples of convert array to list

# Example 1: Using tolist() function
# Convert the NumPy array to a Python list 
array = np.array([1, 3, 5, 7, 9])
result = array.tolist()

# Example 2: Convert the two-dimensional NumPy array 
# To a nested Python list
array = np.array([[3,6,9], [2,5,8]])
nested_list = array.tolist()

# Example 3: Using tolist() function
# To single statement
nested_list = np.array([[3,5,9], [2,7,8],[3,6,2]]).tolist()

# Example 4: Using list() function
# Convert the NumPy array to a list
array = np.array([1, 3, 5, 7, 9]) 
result = list(array)

# Example 5: Converting 0-D array to list
array = np.array(1)
result = list(array)

# Example 6: Converting a 0D array to list
# Use ndarray.tolist() function
array = np.array(1)
result = array.tolist()

2. Convert a One-Dimensional Array to a List

To convert a one-dimensional NumPy array to a list use tolist() function of the ndarray, First, let’s create an ndarray using array() function and then use tolist() function to convert it to a list. The array() function takes a Python list as an argument.

Note that while converting the array to a list, it converts the items to the nearest compatible built-in Python type.


# Import numpy
import numpy as np

# Convert one-dimensional numpy array to list
array = np.array([1, 3, 5, 7, 9])
print("Original array:\n",array)

# Using tolist() function
# Convert the NumPy array to a Python list 
result = array.tolist()
print("After converting the numpy array to a list:\n",result)
print("Type:\n",type(result))

Yields below output. type() function returns the type of the object.

 convert numpy array list

3. Convert a Two-Dimensional Array to a List

You can convert a two-dimensional NumPy array to a nested Python list, where each row of the array is represented as a sublist in the Python list.

In the below example, the tolist() method is used to convert the two-dimensional array array into a nested Python list nested_list. Each row of the NumPy array becomes a sublist in the Python list.


import numpy as np

# Create a two-dimensional numpy array
array = np.array([[3,6,9], [2,5,8]])
print("Original 2D array:\n",array)

# Convert the two-dimensional NumPy array 
# To a nested Python list
nested_list = array.tolist()
print("After converting an array to a nested list:\n",nested_list)

Yields below output.

 convert numpy array list

Alternatively, you can do it in a single statement.


import numpy as np

# Using tolist() function
# To single statement
nested_list = np.array([[3,5,9], [2,7,8],[3,6,2]]).tolist()
print("After converting an array to a nested list:\n",nested_list)

Yields below output.


# Output:
After converting an array to a nested list:
 [[3, 5, 9], [2, 7, 8], [3, 6, 2]]

4. Using list() Function

Similarly, You can also use the list() function to convert a one-dimensional NumPy array to a Python list. But this doesn’t convert elements to Python type. hence the values in the list are in numpy types.

In the below example, the list() function is used to convert the one-dimensional array array into a Python list result. The resulting list contains the same elements as the original NumPy array.


import numpy as np

# Convert single-dimensional numpy array to list
array = np.array([1, 3, 5, 7, 9])
print("Original array:\n",array)

# Using list() function
# Convert the NumPy array to list 
result = list(array)
print("After converting the numpy array to a list:\n",result)
print("Type:\n",type(result))
print(type(result[0]))

Yields below output.


# Output:
Original array:
 [1 3 5 7 9]
After converting the numpy array to a list:
 [1, 3, 5, 7, 9]
Type:
 <class 'list'>
<class 'numpy.int64'>

5. Converting 0-D Array to List

If you have a 0-D array (zero-dimensional) and trying to convert it to a list gives you an error, let’s try this with an example. Here array(1) creates a zero-dimensional array.


import numpy as np

# Create numpy array
array = np.array(1)
print("Original array:\n",array)

# Converting 0-D array to list
result = list(array)
print("After converting the numpy array to a list:\n",result)

Yields below output


# OutPut:
TypeError: iteration over a 0-d array

In order to convert the 0D array, use ndarray.tolist() function.


# Converting 0D array to list
# Use ndarray.tolist() function
array = np.array(1)
result = array.tolist()
print("After converting the numpy array to a list:\n",result)
print(type(array.tolist()))

Yields below output.


# OutPut:
After converting the numpy array to a list:
 1
<class 'int'>

Frequently Asked Questions

How do I convert a NumPy array to a Python list?

You can convert a NumPy array to a Python list, you can use either the tolist() method of the NumPy array or the list() constructor.

What happens if my NumPy array is multi-dimensional?

If your NumPy array is multi-dimensional, the tolist() method will recursively convert the array into nested lists. Each dimension of the NumPy array will correspond to a nested list in the resulting Python list.

Are there any differences between the tolist() method and the list() constructor when converting NumPy arrays to lists?

Both methods serve the same purpose of converting NumPy arrays to Python lists, and they produce the same output. The choice of method often depends on personal preference or specific use cases. The tolist() method is specific to NumPy arrays, whereas the list() constructor is a built-in Python function that can convert any iterable (including NumPy arrays) to a list.

Can I convert a NumPy array of strings to a Python list of strings?

You can convert a NumPy array of strings to a Python list of strings. You can use either the tolist() method of the NumPy array or the list() constructor. Both methods will handle string elements and convert them into a Python list of strings.


Can I convert a multi-dimensional NumPy array to a nested Python list?

You can convert a multi-dimensional NumPy array to a nested Python list using the tolist() method provided by NumPy. Each dimension of the NumPy array will be represented as a nested list.

Conclusion

In this article, I have explained how to convert aarray to a list by using tolist() and list() methods with examples. If the array is one-dimensional, a list with the array elements is returned (list of objects). For a multi-dimensional array, a nested list is returned (list of list of objects).

Happy Learning!!

References