Array | Data Structure

If you are familiar with at least one programing language, you already used array. It’s a linear and the most fundamental data structure of computer science. It’s comparatively easy to learn and very useful.

In short, an array is a collection of elements. These elements can be any type of data type. Such as integer, string, float, boolean, function, object, or array itself. They may vary from language to language.

In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements, each identified by at least one array index or key.

Wikipedia

Before Start

In this article, we will use Python as a programming language to illustrate various examples of an array. There is no traditional array in Python. We will use Python’s list which is equivalent to Array. Using Python’s list, we can do everything we can with this array.

In statically typed languages we have to declare the type of data and also need to mention the size of the array. These are called fixed size or fixed type array. In Python, we don’t need anything of that. Python does this for us dynamically. There are some advantages and disadvantages of the static and dynamic array. But we will not cover that in this article.

Simple Array

We use an array because of storing one or more values. Sometimes it’s very tedious to create a variable for each value. If we want to store all prime numbers from 1 to 10, it will not be a wise decision to declare a variable for each number. It should not be. We can easily store them in an array. Let’s take a look at a basic example of an array (list in pyhon).

primes = [2, 3, 5, 7, 9]  # All prime numbers from one to ten stored in one variable
print(primes) # Output = [2, 3, 5, 7, 9]

# retrieve value
print(primes[0]) # Output = 2
print(primes[2]) # Output = 5
print(primes[-1]) # Output = 9

An array is the index base. We can access the array elements using their index. In most programming languages, index counting starts at zero. Except for some language like FORTRAN, Lua, Erlang, etc.

Look at the above example, we can access our list’s element using the index number between square brackets. To get the first element index will be 0, index 1 for the second element, index 2 for the third one, and so on. We can also get the last element using -1.

Array elements are stored sequentially in the computer memory. That’s why we can access an element by index. The complexity of accessing the value in the array is O(1).

Python is a dynamic programing language. In any dynamic language, we can store different types of data in same array. Look at the below example.

lst = [0, 'Hello', 0.0, True] # different type value in one list
print(lst) # Output = [0, 'Hello', 0.0, True] 

# Printing all elements
for el in lst:
    print(el)

# 0
# Hello
# 0.0
# True

Some Useful Operation

Retrieve data: It’s easy to retrieve data from an array/list. If we need specific data, we can get by their index. I already show an example of that. If we need to get all data, we get using a loop. See more examples below.

lst = [1, 2, 3, 4, 5, 6, 7]

print(lst[0]) # 1
print(lst[3]) # 4

# print(lst[7]) IndexError: list index out of range

# Printing all elements
for i in range(len(lst)):
    print(f'Index = {i}, Value = {lst[i]}')

Add/Update Data: We have to use a method of python to add data in a list. Which is append(). This method puts the data at the end of the list. There is another method we can use if we want to add data in any position of the list. Which is insert(). Let’s see more details on the example.

lst = [1, 2, 3, 4, 5, 6, 7]

print(lst) # [1, 2, 3, 4, 5, 6, 7]

lst.append(8)
print(lst) # [1, 2, 3, 4, 5, 6, 7, 8]

lst.insert(0, 0) # insert(position, value)
print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8]

# We can also update using index
lst[1] = 11
print(lst) # [0, 11, 2, 3, 4, 5, 6, 7, 8]

Delete Data: It’s easy. We have to use another method to delete data from a list. Which is remove(). If there is more than one same value in the list, then the leftmost side will be deleted. Let’s see.

lst = [1, 2, 3, 4, 7, 5, 6, 7]
print(lst) # [1, 2, 3, 4, 7, 5, 6, 7]

lst.remove(1)
print(lst) # [2, 3, 4, 7, 5, 6, 7]

lst.remove(7)
print(lst) # [2, 3, 4, 5, 6, 7]

Size: To know the total elements of an array we can use len() function.

lst = [1, 2, 3, 4, 7, 5, 6, 7]
print(len(lst)) # 8

I hope you got a decent idea about the array data structure. There are so many things to cover. It’s difficult for me to do this in one article. Finally, I hope you enjoy this article.