In this lesson, we will focus on Python list pop method. We will learn how to remove any member of a list in python. We can do this with python pop method and the index of the list member. You can also check python list append method to see how to add a new member at the end of a list.
Let’s show how can we use python pop method for python list pop.
elves = ["Arwen", "Legolas", "Elrond", "Galadriel"] elves.pop(2) print(elves)
With this example, we will remove the third member of the list (index = 2). Remember, the indexes starts from 0 and continues as 1,2,3 etc. So the first member has index 0, the second memebr has index 1 and the third member has index 2. Here, we will remove third member, index 2. So, the output of this python code will be:
['Arwen', 'Legolas', 'Galadriel']
You can also learn other Python List Methods
You can also learn how to find Lenth of a Python List
We can also assign the removed member to a variable and then we can print it like below:
elves = ["Arwen", "Legolas", "Elrond", "Galadriel"] RemovedMember=elves.pop(2) print(RemovedMember)
The output of this code will be the removed member:
Let’s do the similar examples with number lists.
numbers = [14,26,45,76,95] numbers.pop(3) print(numbers)
As an output, the fourth member of the list (index=3), “76”, will be removed.
[14, 26, 45, 95]
Here, we have used python pop () method for Python list pop function. You can use this method for your various python codes related with lists. You can find other list lessons in this Python Training.