# How to Convert NumPy Array to List

Here are ways to convert a NumPy array to a list:

1. Using tolist() Method
2. Using list() constructor
3. Using list comprehension
4. Using a for loop

## Method 1: Using tolist() Method

The straightforward and efficient way is to use the tolist() method, which is particularly useful for multi-dimensional arrays when you want to maintain the nested structure, although it can also be used for one-dimensional arrays.

### Example 1: One Dimensional Array

``````import numpy as np

# Create a NumPy array
my_array = np.array([2, 4, 6, 8])

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = my_array.tolist()

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list: [2 4 6 8]
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [2, 4, 6, 8]
Type after conversion: <class 'list'>``````

In the above example, we used the type() function to print the array’s type.

### Example 2: Muti-dimensional Array

``````import numpy as np

# Create a NumPy array
my_array = np.array([[2, 4], [6, 8]])

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = my_array.tolist()

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list:
[[2 4]
[6 8]]
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [[2, 4], [6, 8]]
Type after conversion: <class 'list'>``````

## Method 2: Using list() constructor

The list() constructor takes a NumPy array as an argument and returns a list.

### Example

``````import numpy as np

# Create a NumPy array
my_array = np.array([2, 4, 6, 8])

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = list(my_array)

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list: [2 4 6 8]
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [2, 4, 6, 8]
Type after conversion: <class 'list'>``````

### What happens if the input is a 0-dimensional array (a single element)?

``````import numpy as np

# Create a NumPy array
my_array = np.array(2)

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = list(my_array)

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``TypeError: iteration over a 0-d array``

As you can see, trying to convert a 0-dimensional array directly using list() throws a TypeError, because it can’t convert it.

You should use the np.array.item() method to extract the Python scalar value.

``````import numpy as np

# Create a NumPy array
my_array = np.array(2)

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = [my_array.item()]

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list: 2
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [2]
Type after conversion: <class 'list'>``````

## Method 3: Using list comprehension

List comprehension iterates over each element in an array and creates a new list containing the same elements.

### Example

``````import numpy as np

# Create a NumPy array
my_array = np.array([2, 4, 6, 8])

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

converted_list = [item for item in my_array]

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list: [2 4 6 8]
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [2, 4, 6, 8]
Type after conversion: <class 'list'>``````

## Method 4: Using a for loop

You can loop through the array and manually append each element to a new list using the append() method.

### Example

``````import numpy as np

# Create a NumPy array
my_array = np.array([2, 4, 6, 8])

print("Before converting to list:", my_array)
print("Type before conversion:", type(my_array))

# Initialize an empty list
converted_list = []

# Loop through each element in the NumPy array and append it to the list
for i in my_array:
converted_list.append(i)

print("After converting to list:", converted_list)
print("Type after conversion:", type(converted_list))``````

Output

``````Before converting to list: [2 4 6 8]
Type before conversion: <class 'numpy.ndarray'>
After converting to list: [2, 4, 6, 8]
Type after conversion: <class 'list'>``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.