Python Array

Note: When people say arrays in Python, more often than not, they are talking about Python lists. If that's the case, visit the Python list tutorial.

In this tutorial, we will focus on a module named array. The array module allows us to store a collection of numeric values.


Creating Python Arrays

To create an array of numeric values, we need to import the array module. For example:

import array as arr
a = arr.array('d', [1.1, 3.5, 4.5])
print(a)

Output

array('d', [1.1, 3.5, 4.5])

Here, we created an array of float type. The letter d is a type code. This determines the type of the array during creation.

Commonly used type codes are listed as follows:

Code C Type Python Type Min bytes
b signed char int 1
B unsigned char int 1
u Py_UNICODE Unicode 2
h signed short int 2
H unsigned short int 2
i signed int int 2
I unsigned int int 2
l signed long int 4
L unsigned long int 4
f float float 4
d double float 8

We will not discuss different C types in this article. We will use two type codes in this entire article: i for integers and d for floats.

Note: The u type code for Unicode characters is deprecated since version 3.3. Avoid using as much as possible.


Accessing Python Array Elements

We use indices to access elements of an array:

import array as arr
a = arr.array('i', [2, 4, 6, 8])

print("First element:", a[0])
print("Second element:", a[1])
print("Last element:", a[-1])

Output

First element: 2
Second element: 4
Last element: 8

Note: The index starts from 0 (not 1) similar to lists.


Slicing Python Arrays

We can access a range of items in an array by using the slicing operator :.

import array as arr

numbers_list = [2, 5, 62, 5, 42, 52, 48, 5]
numbers_array = arr.array('i', numbers_list)

print(numbers_array[2:5]) # 3rd to 5th
print(numbers_array[:-5]) # beginning to 4th
print(numbers_array[5:])  # 6th to end
print(numbers_array[:])   # beginning to end

Output

array('i', [62, 5, 42])
array('i', [2, 5, 62])
array('i', [52, 48, 5])
array('i', [2, 5, 62, 5, 42, 52, 48, 5])

Changing and Adding Elements

Arrays are mutable; their elements can be changed in a similar way as lists.

import array as arr

numbers = arr.array('i', [1, 2, 3, 5, 7, 10])

# changing first element
numbers[0] = 0    
print(numbers)     # Output: array('i', [0, 2, 3, 5, 7, 10])

# changing 3rd to 5th element
numbers[2:5] = arr.array('i', [4, 6, 8])   
print(numbers)     # Output: array('i', [0, 2, 4, 6, 8, 10])

Output

array('i', [0, 2, 3, 5, 7, 10])
array('i', [0, 2, 4, 6, 8, 10])

We can add one item to the array using the append() method, or add several items using the extend() method.

import array as arr

numbers = arr.array('i', [1, 2, 3])

numbers.append(4)
print(numbers)     # Output: array('i', [1, 2, 3, 4])

# extend() appends iterable to the end of the array
numbers.extend([5, 6, 7])
print(numbers)     # Output: array('i', [1, 2, 3, 4, 5, 6, 7])

Output

array('i', [1, 2, 3, 4])
array('i', [1, 2, 3, 4, 5, 6, 7])

We can also concatenate two arrays using + operator.

import array as arr

odd = arr.array('i', [1, 3, 5])
even = arr.array('i', [2, 4, 6])

numbers = arr.array('i')   # creating empty array of integer
numbers = odd + even

print(numbers)

Output

array('i', [1, 3, 5, 2, 4, 6])    

Removing Python Array Elements

We can delete one or more items from an array using Python's del statement.

import array as arr

number = arr.array('i', [1, 2, 3, 3, 4])

del number[2]  # removing third element
print(number)  # Output: array('i', [1, 2, 3, 4])

del number  # deleting entire array
print(number)  # Error: array is not defined

Output

array('i', [1, 2, 3, 4])
Traceback (most recent call last):
  File "<string>", line 9, in <module>
    print(number)  # Error: array is not defined
NameError: name 'number' is not defined

We can use the remove() method to remove the given item, and pop() method to remove an item at the given index.

import array as arr

numbers = arr.array('i', [10, 11, 12, 12, 13])

numbers.remove(12)
print(numbers)   # Output: array('i', [10, 11, 12, 13])

print(numbers.pop(2))   # Output: 12
print(numbers)   # Output: array('i', [10, 11, 13])

Output

array('i', [10, 11, 12, 13])
12
array('i', [10, 11, 13])

Check this page to learn more about Python array and array methods.


Python Lists Vs Arrays

In Python, we can treat lists as arrays. However, we cannot constrain the type of elements stored in a list. For example:

# elements of different types
a = [1, 3.5, "Hello"] 

If you create arrays using the array module, all elements of the array must be of the same numeric type.

import array as arr
# Error
a = arr.array('d', [1, 3.5, "Hello"])

Output

Traceback (most recent call last):
  File "<string>", line 3, in <module>
    a = arr.array('d', [1, 3.5, "Hello"])
TypeError: must be real number, not str

When to use arrays?

Lists are much more flexible than arrays. They can store elements of different data types including strings. And, if you need to do mathematical computation on arrays and matrices, you are much better off using something like NumPy.

So, what are the uses of arrays created from the Python array module?

The array.array type is just a thin wrapper on C arrays which provides space-efficient storage of basic C-style data types. If you need to allocate an array that you know will not change, then arrays can be faster and use less memory than lists.

Unless you don't really need arrays (array module may be needed to interface with C code), the use of the array module is not recommended.

Did you find this article helpful?