Arrays in Python | Declaration and Operations

15 min read

An array is a collection of homogeneous elements stored at contiguous memory locations. Know more about Arrays here.

 

Array Declaration in Python

Arrays in python are handled using a module called array. On creating arrays using the array module, all the elements in the array will be of the same data type. Lists can be also treated as arrays. But, the elements in a list can be of any data type whereas array elements are of the same data type.

array(data-type, values) // creates an array with the specified data type and elements.

# A sample Python program
import array as arr 
# creating an array with integer type 
int_array = arr.array('i', [5, 10, 15, 20]) 

# printing original array 
print ("The new created array is : ") 
for i in range (0, 4): 
print (int_array[i]) 
print() 

# creating an array with float type 
float_array = arr.array('d', [0.5, 1.5, 2.5, 3.5]) 

# printing original array 
print ("The new created array is : ") 
for i in range (0, 4): 
print (float_array[i]) 

OUTPUT

creating-python-arrays

 

Basic Operations in Arrays

  • Insertion − Adds an element at the position specified.
  • Deletion − Deletes the element specified.
  • Search − Searches an element in the array using the given index or the value.
  • Update − Updates an element at the specified index.
  • Slicing − Slices array elements of given index from the array.

Insertion

import array as arr 
int_array = arr.array('i', [5, 10, 15, 20]) 
print ("Array before insertion : ") 
for i in range (0, 4): 
print (int_array[i]) 
print() 

# inserting array using insert() function 
int_array.insert(2, 7) 

print ("Array after insertion :") 
for i in (int_array): 
print (i) 
print() 


OUTPUT


insertion-python-arrays

Deletion

import array as arr
int_arr = arr.array('i', [5, 10, 15, 20, 25]) 
# printing original array 
print ("The new created array is :") 
for i in range (0, 5): 
print (int_arr[i]) 
int_arr.remove(15)
# printing array after deleting
print ("The array after deletion is : ") 
for i in range (0, 4): 
print (int_arr[i]) 


OUTPUT

deletion-python-array

Searching

import array as arr
int_arr = arr.array('i', [5, 10, 15, 20, 25]) 
flag = 0
# printing original array 
print ("The new created array is : ") 
for i in range (0, 5): 
print (int_arr[i], end =" ") 
search_elem = 15
for i in range (0, 5):
if(int_arr[i] == search_elem):
flag = 1
k = i
if(flag == 1):
print("\nElement Found at index : ", end = '' )
print(k)
else:
print("\nElement not found")


OUTPUT

searching-python-arrays

 

Updating

import array as arr
int_arr = arr.array('i', [5, 10, 15, 20, 25]) 
# printing original array 
print ("The new created array is : ") 
for i in range (0, 5): 
print (int_arr[i]) 
int_arr[2] = 35
int_arr[4] = 100
# printing array after updating
print ("The array after updation : ") 
for i in range (0, 5): 
print (int_arr[i])


OUTPUT

updation-python-arrays

 

Slicing


import array as arr
int_arr = arr.array('i', [5, 10, 15, 20, 25, 30, 35, 40, 45])
print("Intial Array: ")
for i in (int_arr):
print(i,end = " ")

sliced_array = int_arr[2:6]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array) sliced_array = int_arr[4:]
print("\nElements sliced from 4th "
"element till the end: ")
print(sliced_array)

sliced_array = int_arr[:]
print("\nPrinting all elements using slice operation: ")
print(sliced_array)


OUTPUT

slicing-python-arrays

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error