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
Featured Post
Creating Games in Scratch for ICSE Class 6
An Introduction to Creating Games in Scratch for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...
-
These are several steps you need to use in several cases in removing a trojan or a worm manually. You may need to take one or more of these ...
-
Today we will learn about an Educational App that introduces names of Fruits and Vegetables to kids. This app is developed by a company nam...
-
An Introduction to Creating Games in Scratch for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...