Loading...

Python Arrays


in this tutorial we are going to learn what are arrays in python, how to create arrays, how to access items in an array, how to remove items in an array in python


What are arrays in python ?


in python there is no data type for arrays but we can use list for that specific purpose.
if you don't about anything what a list in python then checkout python list
Arrays
it is a collection of similar type of element (items).
it is used in real life scenarios such as in shopping cart which conatins many items in it.
for example:
        		     					    		
cars = ['swift dzire', 'forturner', 'lambhorghini', 'ferrari']

products = ['samsung s20', 'iphone 9', 'pixel 3a', 'samsung z flip', 'mate 20 pro']

teams = ['team dolphins', 'team cobras', 'team kangarros', 'team lion']
        		     					    		
        		     					    	
in the above examples you notice that each variables contains only similar elements in it.
- cars variable contains only car items in it.
- products variable contains only product items in it.
- teams variable contains only team items in it.


How to access array elements (items) in python ?


in python we acces array elements (items) by using its index position number such as 0, 1, 2 etc.
index position number always starts from 0 to n numbers.
so to access the first item it is at the index position 0.
for example:
        		     					    		
cars = ['swift dzire', 'camry', 'honda city', 'audi r1']

# remember array starts from 0.
# last car at index position 3. 
print("name of the last car is : ", cars[3])
 
products = ['iphone se 2', 'samsung s20 ultra', 'pixel 3', 'mate 30 pro']

# second product at position 1.
print("name of the second prod item is : ", products[1])

output:

name of the last car is :  audi r1
name of the second  prod item is :  samsung s20 ultra

        		     					    		
        		     					    	



How to insert or add array elements (items) in python ?


in python we add an item in array by using built-in append() function.
when we use append() function it will add item in the last position.
for example:
        		     					    		
cars = ['audi a3', 'toyota fortuner', 'suzuki ciaz']

cars.append('lamborghini huracan')

print(cars)

output:

['audi a3', 'toyota fortuner', 'suzuki ciaz', 'lamborghini huracan']

        		     					    		
        		     					    	



How to remove or delete array elements (items) in python ?


in python we can remove or delete array items by using following ways.
by using del keyword
we can remove a single item or an array variable completely by using del keyword.
it remove an item if its index position is mentioned otherwiese it will delete an array completely and makes it undefined.
for example:
        		     					    		
cars = ['audi a3', 'toyota fortuner', 'suzuki ciaz']

del cars[0]

print(cars)

output:

['audi a3', 'toyota fortune', 'suzuki ciaz', 'lamborghini huracan']

        		     					    		
        		     					    	
by using remove() function
this function is used when we don't know the index position of the item but only knows its value.
it works only on values present in an array.
for example:
        		     					    		
cars = ['audi a3', 'toyota fortuner', 'suzuki ciaz']

cars.remove('toyota fortuner')

print(cars)

output:

['audi a3', 'suzuki ciaz']

        		     					    		
        		     					    	
by using pop() function
this function removes the last item in an array.
it removes the recently added item in an array.
for example:
        		     					    		
cars = ['audi a3', 'toyota fortuner', 'suzuki ciaz']

cars.pop()

print(cars)

output:

['audi a3', 'toyota fortuner']

        		     					    		
        		     					    	



How to clear all the array elements (items) in python ?


in python we can clear in an array by using built-in clear() function.
when we use this function on the array all the elements are removed and the array variable becomes empty.
for example:
        		     					    		
cars = ['lambhorghini huracan', 'kia seltos', 'suzuki ciaz']

print(cars)

cars.clear()

# after calling clear function

print("after clear functin : ", cars)

output:

['lambhorghini huracan', 'kia seltos', 'suzuki ciaz']
after clear functin :  []