Tutorial

How To Convert a NumPy Array to List in Python

Updated on September 24, 2022
author

Pankaj

How To Convert a NumPy Array to List in Python

Introduction

With NumPy, np.array objects can be converted to a list with the tolist() function. The tolist() function doesn’t accept any arguments. If the array is one-dimensional, a list with the array elements is returned. For a multi-dimensional array, a nested list is returned.

Prerequisites

In order to complete this tutorial, you will need:

This tutorial was tested with Python 3.9.6 and NumPy 1.23.3.

Converting one-dimensional NumPy Array to List

Let’s construct a one-dimensional array of [1, 2, 3]:

import numpy as np

# 1d array to list
arr_1 = np.array([1, 2, 3])

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

This code will output:

NumPy Array:
[1 2 3]

Now, let’s use tolist():

import numpy as np

# 1d array to list
arr_1 = np.array([1, 2, 3])

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

list_1 = arr_1.tolist()

print(f'List: {list_1}')

This new code will output:

List: [1, 2, 3]

The array has been converted from numpy scalars to Python scalars.

Converting multi-dimensional NumPy Array to List

Let’s construct a multi-dimensional array of [ [1, 2, 3], [4, 5, 6] ]:

import numpy as np

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

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

This code will output:

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

Now, let’s use tolist():

import numpy as np

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

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

list_2 = arr_2.tolist()

print(f'List: {list_2}')

This new code will output:

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

The array has been converted from numpy scalars to Python scalars.

Conclusion

In this article, you learned how to use tolist() to convert np.array objects to lists. It is applicable to one-dimensional and multi-dimensional arrays.

References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 7, 2020

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

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 22, 2020

    Thank you for this i can convert ndarray to list obj

    - arnaldo

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.