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...
-
ICSE Class 5 Computer Studies This lesson will give students a brief introduction to Scratch programming. I am using Scratch3...
-
In this hour of code exercise "Code with Anna and Elsa" you will learn to draw different geomoetric shapes on ice. There are a ...
-
This blog post is created to support the students taking up the Code Studio courses on Code.org. I have tried to create an answer sheet for ...