How to Convert Array to List in Python

To convert an array to a list in Python, you can use the list() function or the tolist() function.

The following examples show how to convert an array to a list in Python using these two methods.

Using list() Function

We can use the list() function to convert an array to a list.

Suppose we have the following array:

import array

# Create an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# Convert the array to a list
list_ = list(arr)

# Show list
print("List:", list_)

Output: 👇️

List: [1, 2, 3, 4, 5]

In this example, we use the list() function to convert the array arr to a list list_. The output shows the list of numbers converted from the array using the list() function.

Using tolist() Function

We can use the tolist() function to convert a NumPy array to a list.

Suppose we have the following array:

import numpy as np

# Create an array of integers
arr = np.array([1, 2, 3, 4, 5])

# Convert the array to a list
list_ = arr.tolist()

# Show list
print("List:", list_)

Output: 👇️

List: [1, 2, 3, 4, 5]

In this example, we use the tolist() function to convert the NumPy array arr to a list list_. The output shows the list of numbers converted from the array using the tolist() function.

Conclusion

We can use the list() function or the tolist() function to convert an array to a list in Python. These methods provide a convenient way to transform arrays into lists.