Arrays are used to store multiple values in one single variable:
This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Array in Python can be created by importing array module. array(data_type, value_list) is used to create an array with data type and value list specified in its arguments.
a = arr.array ('i' ,[1 , 2 , 3 ])
print ("The new created array is : " , end =" " )
for i in range (0 , 3 ):
print (a[i], end =" " )
print ()
b = arr.array ('d' , [2.5 , 3.2 , 3.3 ])
print ("The new created array is : " , end =" " )
for i in range (0 , 3 ):
print (b[i], end =" " )
OutputThe new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
In order to access the array items refer to the index number.
Use the index operator [ ] to access an item in a array. The index must be an integer.
import array as arr
a = arr.array ('i' , [1 , 2 , 3 , 4 , 5 , 6 ])
print ("Access element is: " , a[0 ])
print ("Access element is: " , a[3 ])
b = arr.array ('d' , [2.5 , 3.2 , 3.3 ])
print ("Access element is: " , b[1 ])
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
Elements can be removed from the array by using built-in remove() function but an Error arises if element doesn’t exist in the set.
Remove() method only removes one element at a time, to remove range of elements, iterator is used.
pop() function can also be used to remove and return an element from the array, but by default it removes only the last element of the array, to remove element from a specific position of the array, index of the element is passed as an argument to the pop() method.
NOTE:Remove method in List will only remove the first occurrence of the searched element.
import array
arr = array.array ('i' , [1 , 2 , 3 , 1 , 5 ])
print ("The new created array is : " , end ="" )
for i in range (0 , 5 ):
print (arr[i], end =" " )
print ("\r" )
print ("The popped element is : " , end ="" )
print (arr.pop (2 ))
print ("The array after popping is : " , end ="" )
for i in range (0 , 4 ):
print (arr[i], end =" " )
print ("\r" )
arr.remove (1 )
print ("The array after removing is : " , end ="" )
for i in range (0 , 3 ):
print (arr[i], end =" " )
Output:The new created array is : 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
In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of elements from the array, we use Slice operation.
Slice operation is performed on array with the use of colon(:).
To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole array in reverse order, use [::-1].
import array as arr
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 =" " )
Sliced_array = a[3 :8 ]
print ("\nSlicing elements in a range 3-8: " )
print (Sliced_array)
Sliced_array = a[5 :]
print ("\nElements sliced from 5th element till the end: " )
print (Sliced_array)
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])
In order to search an element in the array we use a python in-built index() method.
This function returns the index of the first occurrence of value mentioned in arguments.
import array
arr = array.array ('i', [1 , 2 , 3 , 1 , 2 , 5 ])
print ("The new created array is : " , end ="" )
for i in range (0 , 6 ):
print (arr[i], end =" " )
print ("\r" )
print ("The index of 1st occurrence of 2 is : " , end ="" )
print (arr.index (2 ))
print ("The index of 1st occurrence of 1 is : " , end ="" )
print (arr.index (1 ))
Output:The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0
In order to update an element in the array we simply reassign a new value to the desired index we want to update.
import array
arr = array.array('i' , [1 , 2 , 3 , 1 , 2 , 5 ])
print ("Array before updation : " , end ="" )
for i in range (0 , 6 ):
print (arr[i], end =" " )
print ("\r" )
arr[2 ] = 6
print ("Array after updation : " , end ="" )
for i in range (0 , 6 ):
print (arr[i], end =" " )
print ()
arr[4 ] = 8
print ("Array after updation : " , end ="" )
for i in range (0 , 6 ):
print (arr[i], end =" " )
Output:Array before updation : 1 2 3 1 2 5
Array after updation : 1 2 6 1 2 5
Array after updation : 1 2 6 1 8 5
Python has a set of built-in methods that you can use on lists/arrays.
Method | Description |
---|---|
Adds an element at the end of the list | |
Removes all the elements from the list | |
Returns a copy of the list | |
Returns the number of elements with the specified value | |
Add the elements of a list (or any iterable), to the end of the current list | |
Returns the index of the first element with the specified value | |
Adds an element at the specified position | |
Removes the element at the specified position | |
Removes the first item with the specified value | |
Reverses the order of the list | |
Sorts the list | |
Tells the total numbers of elements |