In this Python NumPy tutorial, we will discuss how to convert numpy array to list in Python.
There are multiple methods to convert numpy array to a list in Python, which is shown below:
- Using tolist() function
- Using list() constructor
- Using a list comprehension
- Using the append() method
Convert NumPy Array to List in Python
There are several ways to convert a NumPy array to a Python list, including using the tolist() method, the list() constructor, a list comprehension, or the append() method.
Each method has its own advantages and disadvantages, and the choice of method depends on the specific requirements of your application.
Method-1: Convert NumPy Array to List using tolist() function
The tolist() method is a built-in method of NumPy arrays that converts a NumPy array to a Python list. This method is very straightforward and efficient and is probably the most common way to convert a NumPy array to a list.
# Import the numpy module
import numpy as np
# Create a one-dimensional numpy array
arr1= np.array([2, 4, 5])
# Convert the numpy array to a python list using the tolist() method
list1 = arr1.tolist()
# Print the resulting python list
print(list1)
The above code first imports the NumPy library with the alias np.
- It then creates a one-dimensional NumPy array arr1 containing the values 2, 4, and 5. The tolist() method is then used to convert the NumPy array arr1 to a python list list1.
- Finally, the python list list1 is printed to the console using the print() statement.
Method-2: Convert NumPy Array to List using list() constructor
This method uses the built-in list() constructor in Python to convert a NumPy array to a Python list. The list() constructor takes any iterable object as input and returns a new list object containing all the elements of the iterable.
import numpy as np
# Create a NumPy array
arr = np.array(['USA', 'United Kingdom', 'Canada'])
# Convert the NumPy array to a Python list using the list() constructor
lst = list(arr)
# Print the list
print(lst)
The above code creates a NumPy array containing three string values.
- It then converts the NumPy array to a Python list using the list() constructor. Finally, it prints the list. This code demonstrates how to convert a NumPy array to a Python list.
Output: ['USA', 'United Kingdom', 'Canada']
Method-3: Convert NumPy Array to List using a list comprehension in Python
This method uses a list comprehension to iterate over the elements of the NumPy array and append them to a new list. A list comprehension is a compact way of creating a new list by iterating over an existing iterable object. This method is simple and efficient.
import numpy as np
# Create a NumPy array
arr = np.array(['USA', 'United Kingdom', 'Canada'])
# Convert the NumPy array to a Python list using a list comprehension
lst_ = [x for x in arr]
# Print the list
print(lst_)
The above code demonstrates how to convert a NumPy array to a Python list using a list comprehension.
- First, a NumPy array is created using the np.array() function, which takes a Python list as an argument. The NumPy array contains three string values: ‘USA’, ‘United Kingdom’, and ‘Canada’.
- Next, a list comprehension is used to iterate over each element in the NumPy array and append it to a new Python list.
- The list comprehension has the syntax [expression for item in iterable], where expression is the operation to be performed on each item, item is the current element in the iterable, and iterable is the object being iterated over.
- In this case, the expression is simply the current item
x
, and the iterable is the NumPy array arr. - Finally, the resulting Python list is printed to the console using the print() function.
Output: ['USA', 'United Kingdom', 'Canada']
Method-4: Convert NumPy Array to List using the append() method
This method uses a for loop to iterate over the elements of the NumPy array and append them to a new list using the append() method provided by Python. T
- The append() method is a built-in method in Python that allows you to add elements to the end of a list. This method is simple but less efficient compared to the other methods.
import numpy as np
# Create a NumPy array
arr = np.array(['USA', 'United Kingdom', 'Canada'])
# Convert the NumPy array to a Python list using the append() method
lst = []
for x in arr:
lst.append(x)
# Print the list
print(lst)
The above code demonstrates how to convert a NumPy array to a Python list using a for loop and the append() method.
- First, we create a NumPy array called arr containing the country names ‘USA’, ‘United Kingdom’, and ‘Canada’.
- Next, we create an empty list called lst. We then use a for loop to iterate over each element in arr. On each iteration, we append the current element to the lst using the append() method.
- Finally, we print the resulting list lst which should contain the same elements as the original NumPy array arr.
Output: ['USA', 'United Kingdom', 'Canada']
In this tutorial, we have covered how to convert numpy array to list in Python using the following methods:
- Convert NumPy Array to List using tolist() function
- Convert NumPy Array to List using list() constructor
- Convert NumPy Array to List using a list comprehension
- Convert NumPy Array to List using the append() method
You may like the following tutorials:
- Python NumPy Median() Function
- Numpy Divide in Python
- Python Copy NumPy Array
- Python NumPy genfromtxt()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.