Wednesday

Learn Python Easy way Part 5 Lists

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...