Let us look learn some more ways to work with Python lists.
There one more way to display the element present in a list at a certain location, using the pop method
cities = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities.pop(0)
'Mumbai'
You can use the pop methos without an argument, it will return the last item present in the list
in the above example
cities.pop()
'Calcutta'
You can use the extend method to join two lists together
cities1 = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities2 = ["Kolhapur", "Trivendrum", "Belgaon", "Dibrugadh"]
cities1.extend(cities2)
print(cities1)
['Mumbai', 'Pune', 'Bangalore', 'Calcutta', 'Kolhapur', 'Trivendrum', 'Belgaon', 'Dibrugadh']
We can also use + operator to join two lists
cities1 = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities2 = ["Kolhapur", "Trivendrum", "Belgaon", "Dibrugadh"]
cities1 + cities2
['Mumbai', 'Pune', 'Bangalore', 'Calcutta', 'Kolhapur', 'Trivendrum', 'Belgaon', 'Dibrugadh']
To find out the position of an element in a list
we can use the index method
sweets = ["Rasgulla", "Chamcham", "Basundi", "Balushahi", "Peda"]
sweets.index("Peda")
4