Array Data Structures in Python

Array Data Structures in Python

·

7 min read

An array is a fundamental data structure that is available in most computer languages and has a wide range of applications across many algorithms. The following are some keywords to know in order to grasp the notion of Array:

  1. Element: Each object contained in an array is referred to as an element.

  2. Index: Each element in an array has a numerical index that is used to identify the element.

Fundamental Array Operations

The fundamental operations that an array may do are listed below.

Traverse: prints each array element one at a time.

Insertion: adds a new element at the specified index.

Deletion: removes an element at the specified index.

Search: Searches for an element using the specified index or value.

Update: Updates an element at the specified index.

Creating Arrays in Python
Importing the array module allows you to build an array in Python. array(data type, value list) is a function that generates an array based on the data type and value list supplied in its parameters.

# Python program to demonstrate the creation of Array
# importing "array" for array creations
import array as arr

# creating an array with integer type
a = arr.array('i', [3, 4, 5])

# printing original array
print ("The new array is : ", end =" ")
for i in range (0, 3):
    print (a[i], end =" ")
print()

# creating an array with float type
b = arr.array('d', [2.0, 2.1, 2.2])

# printing original array
print ("The new array is : ", end =" ")
for i in range (0, 3):
    print (b[i], end =" ")

Array Element Addition
The built-in insert() method may be used to add elements to the Array. Insert is a function that is used to insert one or more data elements into an array. Depending on the situation, a new element can be added to the array's beginning, end, or any provided index. append() may also be used to append the value specified in its arguments to the end of an array. This operation has a time complexity of O(n).

# Python program to demonstrate adding Elements to an Array

# importing "array" for array creations
import array as arr

# array with int type
a = arr.array('i', [2, 3, 3, 4])


print ("Array before insertion : ", end =" ")
for i in range (0, 3):
    print (a[i], end =" ")
print()

# inserting array using insert() function
a.insert(2, 5)

print ("Array after insertion : ", end =" ")
for i in (a):
    print (i, end =" ")
print()

# array with float type
b = arr.array('d', [2.0, 3.1, 3.7])

print ("Array before insertion : ", end =" ")
for i in range (2, 4):
    print (b[i], end =" ")
print()

# adding an element using append()
b.append(5.0)

print ("Array after insertion : ", end =" ")
for i in (b):
    print (i, end =" ")
print()

Using the Array to get at items
Refer to the index number to access the array entries. To retrieve an item in an array, use the index operator []. The index must be a positive integer. This operation has a time complexity of O(1).

# Python program to demonstrate accessing of an element from a list

# importing array module
import array as arr

# array with int type
a = arr.array('i', [5, 6, 7, 8, 9])

# accessing element of array
print("Access element is: ", a[0])

Deleting Elements from the array
Elements in the array can be deleted using the built-in remove() method, however, an Error occurs if the element does not exist in the set. The Remove() function only removes one element at a time; an iterator is needed to remove a range of elements. Thus, the time complexity of this function is O(n). The pop() function may also be used to remove and return an element from an array; however, by default, it only removes the final element of the array; to remove an element from a specified place in the array, the index of the element is supplied as an argument to the pop() method. Since the index is supplied, this operation has a time complexity of O(1).

# Python program to demonstrate deletion of elements in a Array

# importing "array" for array operations
import array

# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [10, 20, 30, 40, 50])

# printing original array
print ("The new array is : ", end ="")
for i in range (0, 5):
    print (arr[i], end =" ")

print ("\r")

# using pop() to remove element at 2nd position
print ("The popped element is : ", end ="")
print (arr.pop(2))

# printing array after popping
print ("The array after popping is : ", end ="")
for i in range (0, 4):
    print (arr[i], end =" ")

print("\r")

# using remove() to remove 1st occurrence of 10
arr.remove(1)

# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 3):
    print (arr[i], end =" ")

Slicing an Array
We utilize the Slice operation to display a selected range of elements from the array. The array is used for the slice operation, which is done with the help of a colon (:). To print elements from the beginning to the end of a range, use [:Index], to print elements from the beginning to the end of a range, use [:-Index], to print elements from a specific Index to the end, use [Index:], to print elements within a range, use [Start Index:End Index], and to print the entire List using the slicing operation, use [:]. Use [::-1] to print the entire array in reverse order.

# Python program to demonstrate slicing of elements in a Array

# importing array module
import array as arr

# creating a list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a = arr.array('i', l)
print("Initial Array: ")
for i in (a):
    print(i, end =" ")

# Print elements of a range
# using Slice operation
Sliced_array = a[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array)

# Print elements from a
# pre-defined point to end
Sliced_array = a[5:]
print("\nElements sliced from 5th "
    "element till the end: ")
print(Sliced_array)

# Printing elements from
# beginning till end
Sliced_array = a[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_array)

Searching for an element in an array
To find an entry in an array, we utilize the index() function included in Python. The index of the first occurrence of the value specified in arguments is returned by this function. This operation has a time complexity of O(n).

# Python code to demonstrate searching an element in array


# importing array module
import array

# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [5, 7, 10, 5, 3, 4])

# printing original array
print ("The new created array is : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")

print ("\r")

# using index() to print index of 1st occurrenece of 2
print ("The index of 1st occurrence of 2 is : ", end ="")
print (arr.index(2))

# using index() to print index of 1st occurrenece of 1
print ("The index of 1st occurrence of 1 is : ", end ="")
print (arr.index(1))

Updating Array Elements
To update an array element, just reassign a new value to the index that needs to be updated.

# Python code to demonstrate how to update an element in an array

# importing array module
import array

# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [6, 5, 4, 3, 2, 1])

# printing original array
print ("Array before updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")

print ("\r")

# updating a element in a array
arr[2] = 8
print("Array after updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")
print()

# updating a element in a array
arr[4] = 10
print("Array after updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")