# NumPy Array to List in Python [4 Methods]

Do you want to convert a NumPy array to list? In this Python blog, I will explain how to convert a NumPy array to list in Python using different methods with some examples.

To convert a NumPy array to a list in Python, you can use the np.tolist() method for a straightforward conversion, apply list comprehension for element-wise processing, utilize the list() constructor for direct conversion, or iterate through the array and build a list using the append() method.

## Methods to convert NumPy Array to List in Python

There are four different methods to convert the NumPy array to a list in Python:

1. Using tolist() Method
2. Using list() Constructor
3. Using list Comprehension
4. Using append() Method

Let’s see them one by one with some illustrative examples:

Note: We will check whether the output we get from all the functions is a list or not, with the help of the type() function. The type() function returns the class of the passed argument.

### 1. Python array to list using tolist() function

To convert a NumPy array to list in Python by using the np.tolist() method. This method is a part of the NumPy array object and converts the array into a nested list, preserving the shape of the array. For example:

Case 1: With a One-dimensional array in Python

``````import numpy as np

temperatures = np.array([58.7, 66.3, 52.2])
temp_list = temperatures.tolist()
print(temp_list)
print(type(temp_list))``````

Output:

``````[58.7, 66.3, 52.2]
<class 'list'>``````

Below is an image displaying the results of the code execution in the PyCharm environment.

Case 2: With Multidimensional arrays in Python

``````import numpy as np

rainfall = np.array([
[46.2, 47.5, 45.7, 49.9, 50.3],
[16.5, 14.8, 13.6, 15.2, 17.1],
[61.9, 59.8, 63.3, 62.4, 60.2]
])

rainfall_list = rainfall.tolist()
print(rainfall_list)
print(type(rainfall_list))``````

Output:

``````[[46.2, 47.5, 45.7, 49.9, 50.3], [16.5, 14.8, 13.6, 15.2, 17.1], [61.9, 59.8, 63.3, 62.4, 60.2]]
<class 'list'>``````

The ensuing image captures the results produced by executing the code in PyCharm.

### 2. Numy to list using list() constructor

The list() constructor in Python can be used to convert iterable objects into lists. Since NumPy arrays are iterable, you can use this constructor for the conversion. For example:

``````import numpy as np

gdp = np.array([4.0, 3.5, 5.2, 3.9])
gdp_list = list(gdp)
print(gdp_list)
print(type(gdp_list))``````

Output:

``````[4.0, 3.5, 5.2, 3.9]
<class 'list'>``````

The screenshot below showcases the output generated by running the code within the PyCharm editor.

### 3. Python ndarray to list using list comprehension

List comprehension offers a concise way to create lists. It can also be used to convert a NumPy array to list in Python. This method is particularly useful when we need to apply some operation to the elements while converting them. For instance:

``````import numpy as np

population = np.array([39.14, 29.00, 21.48])
pop_list = [state for state in population]
print(pop_list)
print(type(pop_list))``````

Output:

``````[39.14, 29.0, 21.48]
<class 'list'>``````

The screenshot below visually represents the output from running the code in PyCharm.

### 4. numpy.ndarray to list in Python using append() function

To convert a NumPy array to list in Python, we can use a for loop along with the append() function. This method gives us more control over the process, allowing for additional operations or conditions while converting each element.

READ:  How to Take User Input and Store in Variable using Python Tkinter

We can iterate over the NumPy array with the help of a for loop and then append each item from it to the empty list in Python through the append() function.

For instance:

``````import numpy as np

parks = np.array([9, 8, 5, 4])

parks_list = []
for num in parks:
parks_list.append(num)
print(parks_list)
print(type(parks_list))``````

Output:

``````[9, 8, 5, 4]
<class 'list'>``````

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

## Conclusion

Here, we have learned how to convert a NumPy array to list in Python through various methods, each catering to different scenarios and requirements. Whether we prefer a direct approach with np.tolist(), the simplicity of the list() constructor, the flexibility of list comprehension, or the control provided by manual iteration with append() function, Python offers efficient and versatile ways to transition between NumPy arrays and native lists.

You may also like to read these articles: