Skip to content
Related Articles

Related Articles

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :