Array data structure in python

Home » Data Structure » Array data structure in python

What is an array?

An array is a linear data structure where the same type of data is stored in the consecutive memory address. The data type can be an integer, float, char, string, etc… For example, it can be used to store the student name, employee name, student grade, employee salary, etc…

Types of array

The categorization of the array can be done in two ways. The first one is there are three types of array namely one-dimensional array, two-dimensional array, and multi-dimensional array. While the other is a one-dimensional array and multi-dimensional array. So in this article, I will explain the second way of declaring the types of array.

1. One-dimensional array

It is also known as a 1D array or single dimension array where the data are stored in the sequential order. For example, it can be used to store name or mobile number or grade, etc…

2. Multi-dimensional array

The arrays which have more than one-dimensional are called multi-dimensional array such as a 2D array, 3D array, etc… The best example of a 2D array is a matrix. Anything that can be represented using a matrix can use a 2D array.



Types of operation in the array data structure

  1. Searching – to search an element in the array
  2. Deleting – to delete an element in the array
  3. Inserting – to insert an element in the array
  4. Traversing – to access all the array elements

How to create a 1D array in python

Let’s use the array module from the python library to create an array. It is a python builtin module. You don’t have to install separately. This module has an array class to create an array. It takes two arguments, first the data type and second is the array element and it is optional. If you do not pass the second argument an empty array will be created. Since the array is not having any value in it.

Further, you can access an array element using an index value. The array index value will start from zero. For example, let’s consider the array data = [2,3,4]. You can access the value 2 by using the index zero like data[0], value 3 by data[1], and value 4 by data[2].

from array import array

#declaring and initializing the array
age = array('i', [22,33,12,43,13])

#Traversing the array
for value in age:
    print(value)

#Deleting the element [33] from the array
age.remove(33)
print(age)

#Inserting the element [33] in the array
age.append(33)
print(age)

#Searching the element [33] in the array
print(33 in age)

In the above code, I have used the method such as remove and append to achieve the basic array operations. But remember this is not the only way to do it. You can achieve the same result in different ways and in different methods. Please go through the array module documentation to know more about it.

 

How to create a 2D array in python

An array will have a name and its data type. In python, we don’t have to declare the data type. The interpreter will take care of it during the run time. But in the array module, we pass the data type as it is part of the arguments. The code given below uses the list to implement the array concept. If you want to know more about some built-in data structure in python like list, dictionary, tuple, and set hen you can check out my blog about Basic Data Structure in Python 3

You can access the 2D array element using the index as well but unlike a 1D array, you have to use the row and column value as the index value to access the element in the array. For example, if let’s consider the array data=[[2,3],[8,9]]. You can access the value 2 by data[0][0] and similarly access the value 8 by data[1][0].

#declaring and initializing the array
matrix = [[2,4],[6,8]]

#Traversing the matrix
for row in matrix:
    for value in row:
        print(value)

#Deleting an element [2] from the array
matrix[0].remove(2)
print(matrix)

#Inserting an elment [2] in the array in same position
matrix[0].append(2)
print(matrix)

#Searching an element [2] in the array
for row in matrix:
    for col in row:
        if 2 == col:
            print("True")

Conclusion

Now you know what is an array data structure. How to perform the basic operation on the array. You can implement the array data structure in other programming languages as well such as C programming, java programming, etc… because the underlying logic will remain the same. The only difference will be the syntax that each programming language uses and the builtin module to use the array data structure.

If you are interested to learn about array with context to c programming. Then you might be interested to check out my friend blog on arrays in c programming.