An array is a collection of homogenous elements stored at contagious locations in the memory . Python does not have direct standard data structure for accessing low-level arrays, the closest one is the  list type which is actually implemented through arrays. However, the list type is implemented in such a way that it supports heterogenous types. 

In this article we will see how we can be able to use low-level arrays in Python.

Python offers the array module which  provide an  array data structure that is more efficient than the standard list  when it comes to low-level operations. The array objects strictly supports  only homogenous  low-level types, where the type is specified at instantiation.

Creating arrays

Arrays can be created using the array class.

If you are familiar with arrays from other relatively low-level languages such as C and C++,  you will find array.array() very similar. The class provides an interface to low-level, strongly typed arrays.

The syntax is as follows:

array(typecode, iterable = None)
typecode Required. A code specifying the type of data which is stored in the array.
iterable An iterable (such as a list or tuple) containing values which are convertible to the typecode specified in the first parameter.

The typecode parameter  is a single character used to represent a low-level C type  that the items in the array will be of. The typecodes supported are as shown below:

Type Code C Type minimum size in bytes
b signed integer 1
B unsighned integer 1
u Unicode character 2
h signed integer 2
H unsigned integer 2
i signed integer 2
I unsigned integer 2
l signed integer 4
L unsigned integer 4
q signed integer 8
Q unsigned integer 8
f floating point 4
d floating point 8

To create an array, we have to first import the array module or the array class directly in our program 

Create an array of integers

#import the arry class
from array import array

#Use the 'i' code to specify that it is an array of integer 
arr = array("i", (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

print(arr)

In the above example, we created an array object from the elements of a tuple. If we do not provide the second argument, an empty array will be created.

Create an empty array of integers.

from array import array

arr = array('i')

print(arr)

Array Operations

The arrays shares a lot of functionality with Python lists including indexing, iteration, and deletion of elements.  Just like lists, arrays, are mutable, allowing them to be manipulated in-place.

Indexing and assignment

Arrayscan be indexed with both negative and positive index notations.

indexing in array

from array import array

arr = array('i', range(20))

print(arr[0])
print(arr[1])
print(arr[2])
print(arr[-1])
print(arr[-2])
print(arr[-3])

We can also assign a value at a particular index using the index assignment notation.

Assignment in arrays

from array import array

arr = array('i', range(10))
print('before: ', arr, '\n')

arr[0] = 100
arr[1] = 200
arr[2] = 300
arr[3] = 400

print('after', arr)

Inserting values

We can populate the array with values after instantiation using various methods, such as append(), insert(), and extend().

The append() and insert() methods are used to add a single element to the array. The append() method adds an element at the end of the array, while insert() allows us to insert an element at an arbitrary position.

adding values to the array

from array import array

#an array of unicode characters
arr = array('u')

arr.append('p')
arr.append('y')
arr.append('t')
arr.append('h')
arr.append('o')
arr.append('n')
print(arr)
#the insert method
arr.insert(0, 'P')
print(arr)

The extend() method is used to add multiple elements to the array. It adds all the element of an iterable object at the end of the array. The iterable should contain elements of the supported type.

extend the array with an iterable.

from array import array

arr = array('i', (1, 2, 3, 4, 5))

print('before: ', arr, '\n')
more_items = [6, 7, 8, 9]

arr.extend(more_items)
print('after: ', arr)

Removing elements

We can remove elements from the array using the remove() and pop() methods. The pop() method, removes the element at the specified index, while the remove() method removes the first occurrence of the element given as an argument.

The pop() method removes and returns the element at the end of the array if called without any argument, otherwise, it removes and returns the element at the specified index.

the pop() method

from array import array

arr = array('i', range(10))
#pop at back
print(arr.pop())
print(arr.pop())
print(arr.pop())

#pop at front
print(arr.pop(0))
print(arr.pop(0))
print(arr.pop(0))
print(arr)

the remove() method

from array import array

arr = array('i', [1, 2, 3, 4, 5, 5, 6, 7, 8])
arr.remove(1)
arr.remove(2)
arr.remove(8)
arr.remove(5)

print(arr)

array methods

The methods of array objects and their usage are as summarized below. 

method usage
append(v) Add an element at the end of the array.
count(v) Returns the number of occurrences of element 'v' in the array.
extend(iterable) Add the elements of the 'iterable' at the end of the array.
index(v) Return the index of the first occurrence of element v in the array.
insert(i, v) Insert element v at index i.
pop(i =None) Remove and return the element at index i. If i is not given, removes the item at the last index of the array.
remove(v) Remove the first occurrence of element v in the array.
reverse() Reverse the elements of the array in-place.
itemsize() Returns the length in bytes of one array item in the internal representation
buffer_info() Returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents.
fromlist(L) This method appends items from the list argument to the end of the array
fromunicode(S) This method extends array with data from a Unicode string.
tolist() This method returns a list with elements of  the array.
tounicode() This method returns a string containing the array data represented as a sequence of Unicode characters.

Use methods of the array

from array import array

arr = array('i', range(10))

print(arr.tolist())
print(arr.buffer_info())