The Wayback Machine - https://web.archive.org/web/20220418020917/https://www.geeksforgeeks.org/how-to-convert-numpy-array-to-list/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to convert NumPy array to list ?

  • Last Updated : 16 Mar, 2021

In this article, we will discuss how to convert NumPy arrays to a list. We can convert the Numpy array to the list by tolist() method, we can have a list of data element which is converted from an array using this method.

Syntax: ndarray.tolist()

Parameters: none

Returns: The possibly nested list of array elements.

Approach

  • Import required module.
  • Create array.
  • Display array and class type.
  • Use tolist() method to convert the array.
  • Display list and class type.

Example 1: One Dimensional Array

Python




# import module
import numpy as np
  
# create array
print("\nArray:")
arr = np.array([1, 2, 4, 5])  
print(arr)
print(type(arr))
  
# apply method
lis = arr.tolist()
  
# display list
print("\nList:")
print(lis)
print(type(lis))

Output:

Example 2: Multidimensional Array

Python3




# import module
import numpy as np
  
# create array
print("\nArray:")
arr = np.array([[1, 2, 3], 
                [4, 5, 6], 
                [7, 8, 9]])
print(arr)
print(type(arr))
  
# apply method
lis = arr.tolist()
  
# display list
print("\nList:")
print(lis)
print(type(lis))

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!