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:

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:

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 :.

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.

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.

Output

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

We can also concatenate two arrays using + operator.

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.

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.

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.