Convert NumPy Array to List in Python (4 Examples)
Hi! This tutorial will show you 4 examples where a NumPy array is transformed into a list in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s dive into Python code!
Install & Import NumPy
To install and import Python’s NumPy library, run the lines of code below in your preferred Python coding IDE:
# install numpy pip install numpy # import numpy import numpy as np
Now we can skip to creating a NumPy array for the illustration.
Create Sample NumPy Array
Here, we will create the sample NumPy array that we will turn into a list in this tutorial. Therefore, run the line of code below to create the array.
my_array = np.array([1, 2, 3, 4, 5])
The created NumPy array, my_array, contains 5 integers. Now, let’s convert it to a list of integers!
Example 1: Transform NumPy Array to List Using list() Function
In this first example, we will use Python’s built-in list() function to turn the NumPy array into a list.
my_list = list(my_array) print(my_list) # [1, 2, 3, 4, 5]
You can confirm the class of the list object by calling the type() function as follows.
print(type(my_list)) # <class 'list'>
The result shows that the my_list has, indeed, the list data type. See the second alternative next.
Example 2: Transform NumPy Array to List Using tolist() Method
In this second example, we will transform the NumPy array into a list using NumPy’s tolist() method.
my_list = my_array.tolist() print(my_list) # [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
my_array.tolist(), in essence, says, “turn the array to list”. The result confirms that the attempt is successful.
Example 3: Transform NumPy Array to list Using tolist() Method in List Comprehension
In this third example, we will use NumPy’s
tolist() method inside a list comprehension to transform the NumPy array to a list.
my_list = [i for i in my_array.tolist()] print(my_list) # [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
The list comprehension takes each element inside the NumPy array and places them inside a list. As a result, the list containing the elements in the NumPy array is created.
Example 4: Transform NumPy Array to list Using tolist() Method in a For Loop
In this fourth and final example, we will use NumPy’s
tolist() method inside a for loop, to convert the NumPy array into a list.
my_list =  for i in my_array.tolist(): my_list.append(i) print(my_list) # [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
The for loop, much like the list comprehension demonstrated above, iterates through every element in the NumPy array and places those elements inside a list. The
type() function shows that we have achieved what we want.
We have demonstrated 4 ways of converting a NumPy array to a list. Depending on your use case, you can deploy any of the above methods, mostly in just one line of code.
Video, Further Resources & Summary
Do you need more explanations on how to convert a NumPy array to a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a NumPy array to a list in Python.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Check if String Exists in List in Python (3 Examples)
- Convert List from Character String to Integer in Python (2 Examples)
- Convert List to pandas DataFrame in Python (3 Examples)
- Check if Key Exists in List of Dictionaries in Python (3 Examples)
- Learn Python Programming
This post has shown how to convert a NumPy array to a list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter