Data Structures in Python: Arrays

Satoro
3 min readOct 8, 2021

--

Welcome back! In today’s lecture, we will go through arrays. Arrays are like a container that can store the same type of items. For example, take a look inside your kitchen and check if salt and sugar are in the same box? Of course not! What if you mix these in one box? We all can imagine a hand coming towards your cheek with a large amount of force. Just like that, we are not allowed to put elements with different data types in one array.

Arrays are not popular in python in comparison to other programming languages like C/C++, JavaScript, Java, etc. Instead in python, we mostly work on lists which we will look at in our next lecture. Unlike some other languages, arrays are not built-in to python although we can import them from the array module.

Overview

Instead of declaring individual variables, such as number0, number1, … number10, we can declare one array variable such as numbers. With every element inside of an array, there exists an index(position) of that element.

Array Representation

Array representation
  • Index starts with 0.
  • We can access each element via its index.
  • The length of the array defines the capacity to store the elements.

Array Operations

Some of the basic operations supported by an array:

  • Traverse — To print all the elements one by one.
  • Search —To search an element using the given index or by the value.
  • Insertion — To add an element at the given index.
  • Update — To update an element at the given index.
  • Deletion — To delete an element at the given index.

An array can be created in Python by importing the array module

import array as arr
arrayName = arr.array(typecode, [initializer])

Accessing array elements

In the above code, we import an array and define a variable named “a” which holds the value of the array. We accessed elements to their indices and printed them.

Adding or changing elements of an array

In the above code, we import an array and define a variable named “numbers” which holds the value of the array. To add or change elements just define the particular index of array where you want to operate.

Deleting elements of an array

Elements can be deleted using Python’s del statement. If we want to delete any value from the array we can do that by using the indices of a particular element.

del numbers[2]
print(numbers) #Output: array('i', [0, 2, 12, 13, 9])

Length of array

The length of an array is the number of elements present in an array.

len(numbers)

Array Concatenation

We can easily concatenate two arrays using “+”

numbers2 = arr.array("i", [20, 21, 22])
concArray = arr.array("i")
concArray = numbers + numbers2
print(concArray)
#OUTPUT
array('i', [0, 2, 12, 13, 9, 20, 21, 22])

That’s all for the array lecture, I hope you understand what an array is and how we can work with them. Although we don’t have to work on it much and this is more than enough in Python.

Also, I would suggest you should read python docs for a better understanding of more concepts.

Lectures

Lecture 1: https://medium.com/@saifmdco/data-structures-with-python-introduction-4dadeffa2215

Lecture 2: https://medium.com/@saifmdco/data-structures-with-python-arrays-c498518bf7fd

Lecture 3: https://medium.com/@saifmdco/data-structures-in-python-lists-653a3ad103ab

Lecture 4: https://medium.com/@saifmdco/data-structures-in-python-tuples-3c350640cd9a

Lecture 5: https://medium.com/@saifmdco/data-structures-in-python-dictionary-fad27ffdda8b

Lecture 6: https://medium.com/@saifmdco/data-structures-in-python-2d-array-6bc0154aa717

Lecture 7: https://medium.com/@saifmdco/data-structures-in-python-matrix-22adba5aa597

Lecture 8: https://medium.com/@saifmdco/data-structures-in-python-sets-92d5445e97e8

Lecture 9: https://medium.com/@saifmdco/data-structures-in-python-chainmap-fca2a9d47249

Lecture 10: https://medium.com/@saifmdco/data-structures-in-python-linkedlist-50f2118c659e

Lecture 11: https://medium.com/@saifmdco/data-structures-in-python-stack-6bf182d63581

Lecture 12: https://medium.com/@saifmdco/data-structures-in-python-queue-6361d3dcff0

Lecture 13: https://medium.com/@saifmdco/data-structures-in-python-dequeue-1a585e269a55

Lecture 14: https://medium.com/@saifmdco/data-structures-in-python-custom-stack-b7b9173b4eae

Lecture 15: https://medium.com/@saifmdco/data-structures-in-python-hash-table-39be784eefc1

Lecture 16: https://medium.com/@saifmdco/data-structures-in-python-doubly-linked-list-fe698d74756c

--

--

Satoro
Satoro

Written by Satoro

Python | JS | Tech | Programming | Blogging

No responses yet