Skip to content

Python Array

How to use Python's array module to store collections of numeric values, including creating, accessing, modifying, and comparing arrays with lists.

The array module allows to store a collection of numeric values.

When to use arrays?

Lists are more flexible than arrays because they can store elements of different datatypes.

The array module provides efficient storage space that can't be changed after allocation. This is why arrays are faster and use less memory than lists.

Create Python Arrays

You need to import the array module in order to create an array.

For example:

import array as arr
a = arr.array('d', [1.2, 3.4, 5.6, 7.8])
print(a)

Output:

array('d', [1.2, 3.4, 5.6, 7.8])

Commonly used type codes are listed below:

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

Access Python Array Elements

There are several ways to access the elements of an array.

with Index

The index operator [] is used to access an element in an array.

  • In Python, the index starts from 0.
  • The index must be an integer. Using values of any other type will result in a TypeError.
import array as arr
a = arr.array('i', [2, 4, 6, 8, 10])

print(a[0])  # 2
print(a[1])  # 4

# TypeError! Only integer can be used for indexing
print(a[2.3]) # 

with Negative Index

Python allows negative indexing also for arrays.

The index of -1 refers to the last item, -2 to the second last item and so on.

import array as arr
a = arr.array('i',[0,1,2,3,4,5,6,7,8,9])

# last item
print(a[-1]) # 9

# fifth last item
print(a[-4]) # 6

Slicing

A range of elements in an array can be accessed using the slicing operator :.

The possible cases (with examples) are shown in the table below:

Slice Description Example with a=array('i',[10,20,30,40])
a[from:to] the elements from from to to-1 a[1:3] -> array('i',[20,30])
a[from:] the elements from from to the end of the array a[2:] -> array('i',[30,40])
a[:to] the elements from the beginning of the array to the element to-1 a[:3] -> array('i',[10,20,30])
a[:] all elements a[:] -> array('i',[10,20,30,40])
a[from:to:step] one element each step from from to to-1 a[0:3:2] -> array('i',[10,30])
a[from::step] one item each step from from until the end of the array a[1::2] -> array('i',[20,40])
a[:to:step] one element each step from the beginning to the to-1 element. a[:3:2] -> array('i',[10,30])
a[::step] one item each step a[::2] -> array('i',[10,30])

Array Manipulation

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

Add Array Elements

You can add one item to an array with the append() method or add multiple items with the extend() method.

import array as arr
odd_array = arr.array('i', [1, 3, 5])

odd_array.append(7)  # Append item to odd_list
print(odd_array)     # array('i', [1, 3, 5, 7])
odd_array.extend([9, 11, 13]) # Append list to odd_list
print(odd_array)     # array('i', [1, 3, 5, 7, 9, 11, 13])

As an alternative to the extend() function, the concatenation operator + can be used to combine two arrays.

import array as arr
odd_array = arr.array('i', [1, 3, 5])
even_array = arr.array('i', [2, 4, 6])

# Concat with +
concat_list = odd_array + even_array 
print(concat_list)   # array('i', [1, 2, 3, 4, 5, 6])

Change Array Elements

You can use the assignment operator = to modify an element or range of elements.

a = [2, 4, 6, 8]
a[0] = 1  # change the 1st item 
print(a)  # array('i', [1, 4, 6, 8])

# change 2nd to 4th items
a[1:4] = arr.array('i', [3, 5, 7])
print(a)  # array('i', [1, 3, 5, 7])

Delete Array Elements

You can delete one or more items from an array using Python's del statement. It can also delete the array completely.

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

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

del a  # deleting entire array
print(a)  # NameError: name 'a' is not defined

The remove() function is used to remove the given element and pop() function is used to remove an element at the given index.

import array as arr
a = arr.array('i', [10, 11, 12, 12, 13])

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

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

Lists vs Arrays

In Python, lists can be treated as arrays. However, it is not possible to constrain the type of elements stored in a list.

# a List with elements of different types
a = [1, 2.3, "a string"] 

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, 1.2, "a string"])
# TypeError: must be real number, not str
Navigation