Python Array Module

In this tutorial, we look at the array module present in Python. The array module is used for the efficient handling of numeric values. We look at similarities and differences between lists and arrays.

The Array module

Arrays are similar to lists in Python as they store an ordered collection of items. However, unlike lists, the type of objects stored in arrays is constrained. Consider the example given below.

l1 = ['cat', 23, 45.12]
a1 = array.array('i', [1, 32, 837])
a2 = array.array('d', [23.1, 34.33, 123.35])

‘l1’ is a list. We see that it contains different types of objects (string, int, and float in this case).
a1 is an array. It contains objects of the same type, namely int.
a2 is an array. It contains objects of the same type, namely float.

The array module compactly represents such arrays. It helps us deal efficiently with numeric Python objects. The following type codes are defined.

Python types

To know more details about the array module, please read the official documentation.

Operations using Arrays with implementation

For all operations, we need to first import the array module. It is common practice to import it as arr.

# import the array module
import array as arr

 

Creating Python Arrays

Unlike lists, we need to declare the array object while specifying the Python type.

# declaration of a Python array
a = arr.array('d', [4.12, 323.1, 5])
print(a)

Output:

array('d', [4.12, 323.1, 5.0])

 

Accessing elements in a Python Array

Just as in lists, we can access elements in an array using their indices.
Python uses 0-based based indexing and allows valid negative indices.

print(a[1])  # prints element at index 1
print(a[-3]) # prints element at index -3 
             # i.e. 3rd element from the end of the array

Output:

323.1
4.12

 

Slicing elements in a Python Array

 Just as in lists, we can access a valid range of elements in the array by using the slice (:) operator.

print(a[0:2])    # prints elements starting from index 0 (inclusive) to 
                 # index 2(not inclusive)
print(a[1:])     # prints elements starting from index 1 (inclusive) till 
                 # the end of the array
print(a[:])      # prints all elements from the start till the end of
                 # the array
print(a[::2])    # prints every second element from the start of the 
                 # array till the end of the array
print(a[-1::-2]) # prints every second element from the end of the array                 
                 # going backwards till the start of the array

Output:

array('d', [4.12, 323.1])
array('d', [323.1, 5.0])
array('d', [4.12, 323.1, 5.0])
array('d', [4.12, 5.0])
array('d', [5.0, 4.12])

 

Updating a Python Array

This is again, similar to the operations present for lists as Python Arrays are mutable.
We can update individual elements by accessing their indices.
We can update a range of elements with the help of slicing.
To add a single element to the end of the array, we can use the append() function.
We can insert a single element at an index of the array using the insert() function.
We can add multiple elements to the end of the array using the extend() function.
To insert multiple elements into the array at an index, we can use the slice assignment operation.
We can concatenate arrays using the ‘+’ operator.

# updating one element
a[0] = 12
print(a)

# updating elements in the range 0 to 2
a[:2] = arr.array('d', [21, 213]) 
print(a)

# adding an element to the end of the array
a.append(65.44)
print(a)

# inserting an element at the index 2
a.insert(2, 33.46)
print(a)

# extending the array
a.extend([3993, 377, 200])
print(a)

# inserting multiple elements at the index 4
a[4:4] = arr.array('d', [2, 123.66, 2322]) 
print(a)

# concatenating arrays using '+'
a = a + arr.array('d', [588, 30.22])
print(a)

Output:

array('d', [12.0, 323.1, 5.0])
array('d', [21.0, 213.0, 5.0])
array('d', [21.0, 213.0, 5.0, 65.44])
array('d', [21.0, 213.0, 33.46, 5.0, 65.44])
array('d', [21.0, 213.0, 33.46, 5.0, 65.44, 3993.0, 377.0, 200.0])
array('d', [21.0, 213.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0])
array('d', [21.0, 213.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0, 588.0, 30.22])

 

Searching in a Python Array

We can use the index() function to return the index of the first occurrence of a value in an array.

# displaying the index of the first 
# occurence of 5 in the array
print(a.index(5))

Output:

3

 

Deletion in a Python Array

The del statement is used to delete an element in the array at a given index.
We can use the remove() function to delete the first occurrence of a value in an array.
We also have the pop() function to pop out the element at a given index.
We can also use the del statement to delete a range of elements with the help of the slice operator.
If we need to delete the array itself, that too can be done using del.

# deleting the element at index 1
del a[1]
print(a)

# deleting the first occurrence of 377 in the array
a.remove(377)
print(a)

# popping out the value at index 4 and then printing the array
print(a.pop(4))
print(a)

# deleting the elements in the range 4 to 8
# and then deleting all the elements in the array
del a[4:8]
print(a)
del a[:]
print (a)

# deleting the array
del a
# printing 'a' now will lead to an error 

Output:

array('d', [21.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0, 588.0, 30.22])
array('d', [21.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 200.0, 588.0, 30.22])
123.66
array('d', [21.0, 33.46, 5.0, 2.0, 2322.0, 65.44, 3993.0, 200.0, 588.0, 30.22])
array('d', [21.0, 33.46, 5.0, 2.0, 588.0, 30.22])
array('d')

Conclusion

In this tutorial, we looked at the Array module in Python. We saw how arrays are used to handle numeric values. However, arrays are rarely used when compared to lists as their only advantage is their efficiency in storage. Also, it is not easy to work with arrays for various mathematical operations. If we wish to do so, we should use the help of the NumPy library.

Leave a Reply

Your email address will not be published. Required fields are marked *