Python Arrays

Python Arrays:  An array is a data structure that contains a group of elements. Python arrays are commonly used to organize data so that the related set of values can be easily sorted and searched. These are used to store multiple values in one single variable. You can access the variables by referring to an index number.



Python Arrays

Python Arrays

Python Access

You can access any value using the index number. The index values start from ‘0’ but not 1.

Access Example


Output: Redmi

append( )

The append( ) function is used to add the value which was mentioned in the arguments at the end of the array.

Operation Operator Description
indexed access a[i] ith element
indexed assignment a[i] = x replace ith element
iteration for v in a; assign v to each of the elements in a[ ]
slicing a[i:j] a new array [a[i], a[i+1],…,a[j-1]]
Operation Function call Description
length len(a) number of elements in a[ ]
sum sum(a) sum of elements in a[ ]
minimum min(a) a minimum element in a[ ]
maximum max(a) a maximum element in a[ ]

insert (i, x)

The function is to add the value at the position specified in its arguments.


# Python code to demonstrate the working of  
# array(), append(), insert() 
# importing "array" for array operations 
import array 
# initializing array with array values 
# initializes array with signed integers 
arr = array.array('i', [1, 2, 3])  
# printing original array 
print ("The newely created array is : ",end=" ") 
for i in range (0, 3): 
    print (arr[i], end=" ") 
# using append() to insert new value at end 
# printing appended array 
print("The appended array is : ", end="") 
for i in range (0, 4): 
    print (arr[i], end=" ") 
# using insert() to insert value at specific position 
# inserts 5 at 2nd position 
arr.insert(2, 5) 
# printing array after insertion 
print ("The array after insertion is : ", end="") 
for i in range (0, 5): 
    print (arr[i], end=" ")

The newely created array is:1 2 3
The appended array is:1 2 3 4
The array after insertion is:1 2 5 3 4

Modify Values

You can also modify the values by index numbers.



Output: [‘Oppo’,’Vivo’,’Asus’]

pop( )

The pop( ) function removes the element at postion mentioned in its argument and returns it.

Length of Array

By using the len( ), we can find the length of an array.



Output: 3

Looping Elements of Array

We can use the for in loop to loop through all elements of an array.


for x in mobiles:print(x)


Adding Array elements

You should use the append( ) method to add an element.



Output: [‘Oppo’,’Vivo’,’Asus’,’Nokia’]

Removing Array Elements

We can use the pop( ) method to remove an element.



Output: [‘Oppo’, ‘Asus’]

You can also use the remove( ) to remove the element from Array.



 Output: [‘Oppo’, ‘Vivo’]

Array Methods( )

Python has a set of built-in methods that you can use on Arrays.

Method Description
append( ) Adds an element at the end of the list
clear( ) Removes all the elements from the list
copy( ) Returns a copy of the list
count( ) Returns the number of elements with the specified value
extend( ) Add the elements of a list (or any iterable), to the end of the current list
index( ) Returns the index of the first element with the specified value
insert( ) Adds an element at the specified position
pop( ) Removes the element at the specified position
remove( ) Removes the first item with the specified value
reverse( ) Reverses the order of the list
sort( ) Sorts the list