// Tutorial //

Python - Convert NumPy Array to List

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
Python - Convert NumPy Array to List

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

We can use NumPy np.array tolist() function to convert an array to a list. If the array is multi-dimensional, a nested list is returned. For a one-dimensional array, a list with the array elements is returned.

NumPy Array to List

The tolist() function doesn’t accept any arguments. It’s a simple way to convert an array to a list representation.

1. Converting one-dimensional NumPy Array to List

import numpy as np

# 1d array to list
arr = np.array([1, 2, 3])
print(f'NumPy Array:\n{arr}')

list1 = arr.tolist()
print(f'List: {list1}')

Output:

NumPy Array:
[1 2 3]
List: [1, 2, 3]

2. Converting multi-dimensional NumPy Array to List

import numpy as np

# 2d array to list
arr = np.array([[1, 2, 3], [4, 5, 6]])

print(f'NumPy Array:\n{arr}')

list1 = arr.tolist()

print(f'List: {list1}')

Output:

NumPy Array:
[[1 2 3]
 [4 5 6]]
List: [[1, 2, 3], [4, 5, 6]]

Reference: API Doc


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Pankaj

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

thanks for your help man , great buddy . actualluy i was working in opencv2 and the value was in array and i cant turn it until i found that it was a numpy array . hahahahahah

- akshit

    Thank you for this i can convert ndarray to list obj

    - arnaldo