An array is a data structure which can hold more than one value. It is ordered series of elements of the same type. Arrays behave very much like lists, except that the type of objects stored in them is constrained. array module defines an object type which can compactly represent an array of basic values.

Creating an Array

The following syntax is used to create an array

from array import array
ar = array(data type, value list)

Here, the first parameter is a data type and the values are specified as the next parameter. Data type is specified at object creation time by using a type code, which is a single character. Various data types and their codes are given in below table.

CodeC TypePython TypeMin bytes
bsigned charint1
Bunsigned charint1
uPy_UNICODEUnicode2
hsigned shortint2
Hunsigned shortint2
isigned intint2
Iunsigned intint2
lsigned longint4
Lunsigned longint4
ffloatfloat4
ddoublefloat8

Use indices to access elements of an array. It starts from 0 similar to lists.

Operation on Array

To access array elements, uses index values.

from array import array

# 1. Print Array
a = array('d', [1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
print(a)

# Ouput 
array('d', [1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

# 2. Use indices to access elements of an array:
print("First element:", a[0])
print("Second element:", a[1])
print("Last element:", a[-1])

# Ouput 
First element: 1.1
Second element: 2.2
Last element: 6.6

Add Elements in Array

Using append(), extend() and insert (i,x) functions, new value can be added in an array. append() function is used to add a single element at the end of the array. To add more than one element at the end of array, use the extend() function. This function takes a list of elements as its parameter. To add a specific element at a particular position in the array, use insert(i,x) function. It takes 2 parameters, first parameter is the index where the element needs to be inserted and the second is the value.

from array import array

# Create an array of integer numbers
arr = array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

# 1. Append element 
arr.append(44)
print(arr)

# Output, new value added at the end of it. 
array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 44])

# 2. Add multiple element
arr.extend([55,66,77])
print(arr)

# Output, 3 new elements added to the end of the array.
array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 44, 55, 66, 77])

# 3. Insert an element at index
arr.insert(2, 11)
print(arr)

# Output, resulting array contains the value 11 at the 2nd position in the array.
array('i', [2, 4, 11, 6, 8, 10, 12, 14, 16, 18, 20, 44, 55, 66, 77])

To concatenated any two arrays, use the + symbol.

from array import array

arr1 = array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
arr2 = array('i',[77, 88, 99])

concatArray = array('i')
concatArray = arr1 + arr2
print(concatArray)

# Ouptput
array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 77, 88, 99])

Remove Element from Array

Elements from array can be removed using pop() or remove() method. pop() returns the removed value whereas remove() does not.

pop() function takes either no parameter or the index value as its parameter. It remove the last element and returns it when no parameter is passed. When index is passed, pop() function pops the elements at the given index and returns it. remove() function takes the element value itself as the parameter.

from array import array

arr = array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

# Removes the last value and returns it.
print(arr.pop())
print(arr)

# Output
#20
#array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18])

# Remove the value at the 4th position and returns it
print(arr.pop(3))
print(arr)

# Ouptput
#8
#array('i', [2, 4, 6, 10, 12, 14, 16, 18])

# Remove the element 10
print(arr.remove(10))
print(arr)

# Output
None
array('i', [2, 4, 6, 12, 14, 16, 18])

List vs Array

A list can contain any type of element. However, an array can have single type of of elements. For example:

# List having integer, float and string
a = [1, 3.5, "Hello"]

# All elements of the array must be of the same numeric type, this will throw error
a = array('d', [1, 3.5, "Hello"])

Advantage

Array type provides space efficient storage of basic C-style data types. If you need data structure that you know its data type will not change, then arrays can be faster and use less memory than lists.