Python Arrays

Python Arrays: 

An array is a collection of the same data type together, which is stored in a contiguous memory location. It makes it easier to calculate the position of an element by adding some value to the base value. 

Python arrays can be managed using module arrays.

  • Example: [ 1, 2, 3 ] , [ ‘a’, ‘b’ ] 
    • Not array :- [ 1, ‘a’ ] but it is a list.
Python arrays
  • Array in Python can be created by importing array module. 
  • We can treat a list  as an array.

Syntax: 

module_name.array( data_tyep, values in array)
  • Let’s see some operation on the array and understand the implementation of an array. 

Code #1

#python Program
#Rishikesh 
# Creation of Array 

# importing “array” for array creations 
import array as arr 

# creating an array of integer type 
ar = arr.array(‘i’, [123]) 

# printing original array 
print (“The array is : “end =” “
for i in range (len(ar)): 
    print (ar[i], end =” “
print() 

# creating an array of float type 
br = arr.array(‘d’, [1.55.23]) 

# printing original array 
print (“The array is : “end =” “
for i in range (len(br)): 
    print (br[i], end =” “
    

Output : 

The array is : 1 2 3 
The array is : 1.5 5.2 3.0
type-Code

Inserting element in the array:

  • We can add an element in the array, using  built-in function insert() at any position in the array.
  • We can add an element at end using the append function.

Code #2

#python Program
#Rishikesh 
# Creation of Array 

# importing “array” for array creations 
import array as arr 

# creating an array of integer type 
ar = arr.array(‘i’, [123]) 

# printing original array 
print (“The array before insertion is : “end =” “
for i in range (len(ar)): 
    print (ar[i], end =” “
print() 
ar.insert(1,5)
ar.insert(3,6)
print(‘Array after insertion: ‘, *ar,sep=‘ ‘

ar.append(7

print(‘Array after insertion: ‘, *ar,sep=‘ ‘

Output : 

The array before insertion is : 1 2 3 
Array after insertion: 1 5 2 6 3
Array after insertion: 1 5 2 6 3 7

Removing an element from array:

  • An element can be removed using built-in function remove, it will delete the first occurrence of that element if it is present, else it will raise an error.
  • We can use the pop function to delete from the end of an array. 

Code #3:

#python Program
#Rishikesh 
# Creation of Array 

# importing “array” for array creations 
import array as arr 

# creating an array of integer type 
ar = arr.array(‘i’, [126,5,6,7]) 

# printing original array 
print (“The array before deletion is : “end =” “
for i in range (len(ar)): 
    print (ar[i], end =” “
print() 
#delete the first occurence of the element
ar.remove(6)
print(‘Array after deletion: ‘, *ar,sep=‘ ‘
#remove from last
ar.pop() 

print(‘Array after deletion: ‘, *ar,sep=‘ ‘

#delete any idex 
ar.pop(2
print(‘Array after deletion: ‘, *ar,sep=‘ ‘)  

#some other features
# Slicing 
br=ar[:3]
print(‘the sliced array is:’, br)
#access 
print(‘the index value 2’,ar[2]) 

#updation 
ar[2]=100 
print(‘Array after updation: ‘, *ar,sep=‘ ‘


Output:

The array before deletion is : 1 2 6 5 6 7 
Array after deletion: 1 2 5 6 7
Array after deletion: 1 2 5 6
Array after deletion: 1 2 6
the sliced array is: 1 2 6
the index value 2 6
Array after updation: 1 2 100