An array can be thought of as a container that can hold a fixed number of data values of the same type. Though the use of array is less popular in Python as compared to other languages such as C and Java, most other python data structures internally make use of arrays to implement their algorithms. An array consists of two components, viz Element and Index.
- Element: These are the actual data values to be stored in an array.
- Index: Each element in an array is positioned at the particular location depicted by an index. Python follows zero-based indexing which means an index will always start with 0.
We can create an array by using the built-in
array module. It can be created as follows:
In : from array import * In : arr = array('i', [2, 4, 6, 8]) In : arr Out: array('i', [2, 4, 6, 8]) In : type(arr) Out: array.array
In the above python data structures example, we import the
array method from the
array module and then initialize the variable
arr with values
8 within the square brackets. The
i represents the data type of values. In this case, it represents an integer. Python array documentation provides more information about the various type codes available in Python.
Visualizing an Array
An array declared above can be represented in the following manner:
From the above illustration, the following are the points to be considered.
- Index starts with 0.
- Array length is 4 which means it can store 4 values.
- An array can hold values with a single data type only.
- Each element can be accessed via its index.
Accessing Array Element
We use slicing operation to access array elements. Slicing operation is performed using the square brackets
. It takes an index of an element we are interested in. It can be noticed that the index of the first element in the above array is 0. So, in order to access an element at the position 3, we use the notation
arr to access it.
# Here, 2 represents the index of element 6 In : arr Out: 6 In : arr Out: 2
Let us now understand different array operations in this python data structures tutorial. The
array module provides a wide variety of operations that can be performed based on the requirement. We will learn some of the most frequently used operations.
We use insertion operation to insert one or more data elements into an array. Based on the requirement, an element can be inserted at the beginning, end or any given index using the
# Inserting an element at the beginning In : arr.insert(0, 20) In : arr Out: array('i', [20, 2, 4, 6, 8]) # Inserting an element at the index 3 In : arr.insert(3, 60) In : arr Out: array('i', [20, 2, 4, 60, 6, 8])
An element can be deleted from an array using the built-in
In : arr.remove(20) In : arr Out: array('i', [2, 4, 60, 6, 8]) In : arr.remove(60) In : arr Out: array('i', [2, 4, 6, 8])
We can update an element at the specific index using the assignment operator
= in the following manner:
# Update an element at index 1 In : arr = 1 In : arr Out: array('i', [1, 4, 6, 8]) # Update an element at index 3 In : arr = 7 In : arr Out: array('i', [1, 4, 6, 7])
In addition to the above-mentioned operation, the
array module provides a bunch of other operations that can be carried out on an array such as reverse, pop, append, search, conversion to other types, etc. More details can be found here
Though Python allows us to perform a wide variety of operations on arrays, the built-in array module is rarely used. Instead, in real world programming most programmers prefers to use NumPy arrays provided by the NumPy library.
That’s all for now we’ll continue in the next blog…