Array as a Data structure in Python
An array is a container, which can hold a fixed number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. The important terms to understand the concept of Array are as follows:
Element− Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which is used to identify the element.
Array Representation
Arrays equally may be represented in Row-major form or Column-major form. In Row-major form, all the elements of the first row are printed, then the elements of the second row, and so on up to the last row. Arrays can be declared in various ways in different languages. An illustration is given below:
As per the above illustration, the following are the important points to be considered:
The index begins with 0.
Array length is 10, which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Basic operations supported by an array
In addition to array representation, we shall talk about the basic operations supported by an array. The basic operations supported by an array are as stated below:
Insertion − Adds an element to the given index. Deletion − Deletes an element at the given index. Traverse − Print all the array elements one by one. Search − Searches an element, using the given index or by the value. Update − Updates an element at the given index.
Also, an array is created in Python by importing the array module to the python program. Then, the array is declared as shown below
from array import *
arrayName = array(typecode, [Initializers])
Type Code
In other to create and also print an array in Python, we use the following command
from array import *
array1 = array('i', [10,20,30,40,50])
for x in array1:
print(x)
#When we compile, we shall have as output
10,20,30,40,50
Inserting Operation
Firstly, we have the insert operation. The Insert operation is, to insert one or more data elements into an array. A new element can be added at the beginning, end, or any given index of an array. Here, we add a data element at the middle of the array, using the python in-built insert() method
from array import *
array1 = array('i', [10,20,30,40,50])
#in other to insert 60 in second possition
array1.insert(1,60)
for x in array1:
print(x)
#so our new array will have 10 60 20 30 40 50
Deleting Operation
Secondly, we have the deleting operation. Here we shall look at how to delete or remove an element in an array just by indexing the element. We use the python inbuilt method remove() in order to remove an element independent of the position.
from array import *
array1 = array('i', [10,20,30,40,50])
#method used to remove 40
array1.remove(40)
for x in array1:
print(x)
#so our new output will be an array of 10,20,30,50
Searching Operation
from array import *
array1 = array('i', [10,20,30,40,50])
print (array1.index(40))
#the output here will be 3
Also, You can perform a search for an array element based on its value or its index. Here, we search a data element, by using the python in-built index() method
Update Operation
Furthermore, we have the Update operation refers to updating an existing element from the array at a given index. Here, we simply reassign a new value to the desired index we want to update.
from array import *
array1 = array('i', [10,20,30,40,50])
array1[2] = 80
for x in array1:
print(x)
#our output will then be 10,20,80,40,50
Accessing Array Element
In python, it is possible to access a particular element in an array just by indexing. The code below shows how to access the elements.
from array import *
array1 = array('i', [10,20,30,40,50])
print (array1[0])
#for the first element
print (array1[2])
#for the second element