How to Convert NumPy Array to List

How to convert NumPy Array to 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.

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 a 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 change the NumPy array to a list in Python.


# Below are a quick example

# Convert One-dimensional NumPy Array to List
# Example 1: Use tolist() Method
arr = np.array([3, 6, 9])
list = arr.tolist()
print(f'List: {list}')

# Example 2: Convert Multi-dimensional NumPy Array to List
arr = np.array([[3,6,9], [2,5,8]])
list = arr.tolist()
print(f'List: {list}')

2. Convert NumPy Array to List

To convert a one-dimensional NumPy array to a list use tolist() function of the ndarray, First, let’s create a 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 NumPy array to a list, it converts the items to the nearest compatible built-in Python type.


import numpy as np
# Convert One-dimensional NumPy Array to List
arr = np.array([3, 6, 9])
print(f'NumPy Array:\n{arr}')

# Use tolist() Method
list = arr.tolist()
print(f'List: {list}')
print('Type: '+str(type(list1)))

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


NumPy Array:
[3 6 9]
List: [3, 6, 9]
Type: <class 'list'>

3. Convert Two-dimensional Array to List

If you have a two-dimensional NumPy array the same tolist() function can be used to convert it to List. To create a two-dimensional array, I am passing two python lists as arguments to np.array() function.


import numpy as np
# Convert Multi-dimensional NumPy Array to List
arr = np.array([[3,6,9], [2,5,8]])
print(f'NumPy Array:\n{arr}')

# Use tolist() Method
list = arr.tolist()
print(f'List: {list}')

Yields below output.


NumPy Array:
[[3 6 9]
 [2 5 8]]
List: [[3, 6, 9], [2, 5, 8]]

Alternatively, you can do it in a single statement.


import numpy as np
arr=np.array([[3,5,9], [2,7,8],[3,6,2]]).tolist()
print(arr)

Yields below output.


[[3, 6, 9], [2, 5, 8], [3,6,2]]

4. Using list() Function

Alternatively, you can also use the python list() function to convert NumPy ndarray to list. But this doesn’t convert elements to python type. hence the values in the list are in numpy types.


import numpy as np
a = np.array([1, 2])
a_list = list(a)
print(a_list)
pront(type(a_list))
print(type(a_list[0]))

Yields below output.


[1, 2]
<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
a = np.array(1)
a_list = list(a)

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
arr = np.array(1)
print(arr.tolist())
print(type(arr.tolist()))

Yields below output.


1
<class 'int'>

6. Conclusion

In this article, I have explained how to convert NumPy array 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

You may also like

Leave a Reply

You are currently viewing How to Convert NumPy Array to List