Open In App

NumPy ndarray.tolist() Method | Convert NumPy Array to List

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars.

Data items are converted to the nearest compatible built-in Python type.

Example

Python3




import numpy as np
gfg = np.array([1, 2, 3, 4, 5])   
print(gfg.tolist())


Output

[1, 2, 3, 4, 5]

Syntax

Syntax: ndarray.tolist() 

Parameters: none 

Returns: The possibly nested list of array elements.

How to Convert NumPy Array to Python List

To convert a NumPy array to a Python list we use ndarray.tolist method of NumPy library in Python. It returns a copy of the array data as a Python list (Nested list in case of a multi-dimensional array).

Let us understand it better with an example:

Example:

In this example, we can see that by using ndarray.tolist() method, we can have the list of data items in ndarray.

Python3




# import the important module in python
import numpy as np
        
# make an array with numpy
gfg = np.array([[1, 2, 3, 4, 5],
                [6, 5, 4, 3, 2]])
        
# applying ndarray.tolist() method
print(gfg.tolist())


Output

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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments