Python Arrays: Create, Retrieve, Update and Remove Elements

Why is an Array?

The array is used to store elements of the same data types. Array items are stored using side by side or continuous memory location. This concept is used to store the same type of multiple elements together. There is an array module present in the Python to do array operation. 

Use Of Array

Array is nothing but a special type of variable that is used to store multiple items. Suppose there is a list of items and store every item using an individual variable. If the number of items is 5, the user can remember the variables. But if the number of items is 500, how the user can remember all the variables. The array is the solution that can store a list of items in a single variable.

Creation of an array in python:

There is an array module present in the Python to do array operation. In Python, arrays must be created by using the array module. The below mention way is used to create an array using the array module,

# Python program to create array

import array as array

# Creation of array using integer data type

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

Insertion of value into the array:

Explicitly we can insert or add values or elements in the array. There are some inbuilt methods used in Python to insert elements. There is an inbuilt function in Python, which is called insert() function. This function is used to insert or add elements into the array. Using insert() function elements can be inserted in the first, middle, and end position as per requirements. There is another inbuilt method that is append(). This inbuilt method is used to add value at the end of the array list.

# Import array module to create array
import array as array
arr = array.array('i', [1,2,3,4,5])

# Using insert(), add an element at the index number 2 
arr.insert(2, 6)

# Using append(), add an element at the end of the array
arr.append(7)

Retrieve an element from the array:

Array items are stored using the index numbers. To access or retrieve array elements index numbers are used. The index number should be an integer. Accessing array elements are very easy. A proper index number is needed for this operation.

# Import array module to create array
import array as array

# Creation of array using integer data type
arr = array.array('i', [1,2,3,4,5])

# Accessing of array elemnts
print(“ Elemnet of  0th position is : ”, arr[0])
print(“ Elemnet of  2nd position is : ”, arr[0])
print(“ Elemnet of  5th position is : ”, arr[0])

Removing Array Elements:

Array elements can be removed by using the inbuilt method remove() function. This method removes only one element at a time and it occurs error if the element is not present in the array list. There is another inbuilt function, known as pop(). This function removes the last element. To remove the specific element, the index number of the array element should be passed through the pop() function using arguments.

# Import array module to create array
import array as array

# Creation of array using integer data type
arr = array.array('i', [1,2,3,4,5])

# pop() is used to remove the last element by default and return the popped value
print(arr.pop())

# pop() is used to remove the element at index 4 and return the value
print(arr.pop(4)) 

#remove() is used to remove the value at index number 2 of the array
arr.remove(2)