Python Array - Convert to List


Convert Python Array to List

In Python, you can convert a given array into a list, using tolist() method of array instance.

In this tutorial, you will learn how to convert a given Python array into a list, with examples.

To convert a Python array my_array into a list, call tolist() method on the array instance, as shown in the following code snippet.

my_array.tolist()

The tolist() method returns a new list generated from the items of the given Python array.

1. Convert Integer Array to List

In the following program, we take an integer array my_array with some initial values, and then convert this array into a list using tolist() method.

Python Program

import array as arr

my_array = arr.array('i', [10, 15, 20, 25, 30, 35])

my_list = my_array.tolist()
print(my_list)

Output

[10, 15, 20, 25, 30, 35]

2. Convert Character Array to List

In this example, we shall take a unicode character array, and convert this array into a list.

Python Program

import array as arr

my_array = arr.array('u', 'apple')

my_list = my_array.tolist()
print(my_list)

Output

['a', 'p', 'p', 'l', 'e']

Summary

In this Python Array tutorial, we learned how to convert a given Python Array into a list, using tolist() method, with examples.