Arrays In Python

Introduction

A group of objects kept in consecutive memory regions is known as an array. The goal is to group objects of the same category for storage. As a result, it is simpler to determine each element's position by simply adding an offset to a base value, or the address in memory where the array's first element is stored (usually denoted by the name of the array).

Understanding through an Example

For the sake of simplicity, imagine a flight of stairs with a value (let's say, one of your buddies) placed on each step. Any of your pals may be located here by merely knowing how many steps they have left to go. Python has a module called array that can deal with arrays. When we need to work with only certain data type values, they may be helpful. Lists may be used as arrays by the user. The type of elements kept in a list cannot, however, be restricted by the user. All elements of an array that is created using the array module must be of the same type.

Initializing

Python's array module can be imported to create an array. An array can be created by using the syntax array(data type, value list), which takes two arguments: a data type and a value list.

import array as arr
a = arr.array('i', [1, 2, 3])
print ("array: ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])

print ("array: ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")

Output :
array: 1 2 3
array: 2.5 3.2 3.3

Adding Elements

The Array can have elements added to it by using the built-in insert() method. One or maybe more data elements can be added to an array using the insert command. A new element may be added to the array at its beginning, end, or any specified index. The value specified in its arguments can also be added at the end of the array using the append() function.


import array as arr
# array with int type
a = arr.array('i', [1, 2, 3])
print ("Before Inserting ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# inserting array using insert() function
a.insert(1, 4)
print ("After Inserting : ", end =" ")
for i in (a):
print (i, end =" ")
print()
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
print ("Before Inserting ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")
print()
# adding an element using append()
b.append(4.4)
print ("After Inserting : ", end =" ")
for i in (b):
print (i, end =" ")

Output :
Before Inserting 1 2 3
After Inserting : 1 4 2 3
Before Inserting 2.5 3.2 3.3
After Inserting : 2.5 3.2 3.3 4.4

Accessing elements

Refer to the index number in order to access the array's elements. To retrieve a specific item in an array, use the index operator []. An integer must make up the index.


import array as arr
# array with int type
a = arr.array('i', [1, 2, 3, 4, 5, 6])
# accessing element of array
print("Access element is: ", a[0])
# accessing element of array
print("Access element is: ", a[3])
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
# accessing element of array
print("Access element is: ", b[1])
# accessing element of array
print("Access element is: ", b[2])


Output :
Access element is: 1
Access element is: 4
Access element is: 3.2
Access element is: 3.3

Removing Elements

By utilizing the array's built-in remove() function, elements can be deleted, however if the element doesn't already exist in the set, an error is raised. Iterators are used to delete a range of elements because the Remove() function only removes a single element at a time. However, by default, the pop() function simply removes the final member of the array. To remove an element from a specified place in the array, the element's index is supplied as a parameter to the pop() method. Note: The List's Remove function only eliminates the first instance of the search element.

import array

arr = array.array('i', [1, 2, 3, 1, 5])

print ("array: ", 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 1
arr.remove(1)
# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 3):
print (arr[i], end =" ")


Output:
array: 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5

Slicing of an Array

There are several ways to print the entire array of items in a Python array, but we utilize the Slice operation to display a selected range of elements from the array. The array is subjected to a slice operation using a colon (:). Use [:Index] to print elements from beginning to range, [:-Index] to print elements from range to end, [Index:] to print elements from particular Index to end, [Start Index:End Index] to print elements within range, and [:] to print whole List using slicing operation. Additionally, use [::-1] to print the entire array in reverse order.


import array as arr
# creating a list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = arr.array('i', l)
print("Intial 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)


Output :


Initial Array:
1 2 3 4 5 6 7 8 9 10
Slicing elements in a range 3-8:
array('i', [4, 5, 6, 7, 8])
Elements sliced from 5th element till the end:
array('i', [6, 7, 8, 9, 10])
Printing all elements using slice operation:
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Searching element in a Array

We utilize the index() function that is built into Python to search for a specific element in the array. The index of the first time a value stated in parameters appears is returned by this method.


import array

arr = array.array('i', [1, 2, 3, 1, 2, 5])

print ("array: ", end ="")
for i in range (0, 6):
print (arr[i], end =" ")
print ("r")
# using index() to print index of 1st occurrence of 2
print ("The First spot index is : ", end ="")
print (arr.index(2))
# using index() to print First spot index
print ("The First spot index is : ", end ="")
print (arr.index(1))


Output:

array: 1 2 3 1 2 5
The First spot index is : 1
The First spot index is : 0

Updating Elements in a Array

We just assign a new value to the target index we wish to change to modify an element in the array.


import array


arr = array.array('i', [1, 2, 3, 1, 2, 5])

print ("Values before Update ", end ="")
for i in range (0, 6):
print (arr[i], end =" ")
print ("r")
# updating a element in a array
arr[2] = 6
print("Values after Update ", end ="")
for i in range (0, 6):
print (arr[i], end =" ")
print()
# updating a element in a array
arr[4] = 8
print("Values after Update ", end ="")
for i in range (0, 6):
print (arr[i], end =" ")


Output:
Values before Update 1 2 3 1 2 5
Values after Update 1 2 6 1 2 5
Values after Update 1 2 6 1 8 5

write your code here: Coding Playground