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 [1]: from array import * In [2]: arr = array('i', [2, 4, 6, 8]) In [3]: arr Out[3]: array('i', [2, 4, 6, 8]) In [4]: type(arr) Out[4]: 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 `2`

, `4`

, `6`

, and `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:

Index | 0 | 1 | 2 | 3 |
---|---|---|---|---|

Element | 2 | 4 | 6 | 8 |

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[2]`

to access it.

# Here, 2 represents the index of element 6 In [5]: arr[2] Out[5]: 6 In [6]: arr[0] Out[6]: 2

### Manipulating Arrays

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 `insert()`

method.

# Inserting an element at the beginning In [7]: arr.insert(0, 20) In [8]: arr Out[8]: array('i', [20, 2, 4, 6, 8]) # Inserting an element at the index 3 In [9]: arr.insert(3, 60) In [10]: arr Out[10]: array('i', [20, 2, 4, 60, 6, 8])

An element can be deleted from an array using the built-in `remove()`

method.

In [11]: arr.remove(20) In [12]: arr Out[12]: array('i', [2, 4, 60, 6, 8]) In [13]: arr.remove(60) In [14]: arr Out[14]: 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 [15]: arr[0] = 1 In [16]: arr Out[16]: array('i', [1, 4, 6, 8]) # Update an element at index 3 In [17]: arr[3] = 7 In [18]: arr Out[18]: 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…

## 0 Comments Leave a comment