This is a detailed tutorial of the Python List pop() method. Learn to remove a particular item from the list using its index and return it using list.pop().
Table of Contents
Python List pop()
Method
The pop()
method of the Python List Object is used to remove any item from the list by using its index. This method also returns the item so removed.
Syntax
1 |
removedItem = list.pop(itemIndex) |
pop() Parameters
As shown in the syntax, the pop()
Parameter only takes a single argument i.e. index
of the item that you want to remove from the given Python List.
This argument is optional as the default value of the index parameter is set to -1. In other words, if you will not provide any argument to this method, it will automatically assume the argument to be -1 and hence will remove & return the last element of the list because -1 is the index of the last element of the list.
In case, you passed an invalid index or the index that is actually out of the list boundaries, this method will raise IndexError: pop index out of range
exception.
pop() Return Value
The element or item so removed using the method list.pop(index)
is also returned by this method i.e. the item at the index
position will be returned.
Examples
The following two examples give you more clarity about the usage of the Python List pop() method.
Example 1. Removing a Python List Item using its Index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#A Python List items = ["Pen", "Whiteboard", "Tablet", "Smartphone", "Cleaner"] #Removing the item at Index 2 i.e. "Tablet" removedItem = items.pop(2) #Printing the so returned Removed item print("Removed Item:",removedItem) #Negative Indexing Removals #Another Python List items = ["Laptop","Bottle","Stick","Speaker","Rubber"] #Removing the 3rd Last item from the List #i.e. "Stick" #using Negative Index -3 removedItem = items.pop(-3) print("Removed Item:",removedItem) |
In the code written above, we’ve taken two different python lists. Using the pop()
method, we’re first removing the item at index 2
. The removed item is also returned with this method and hence we’ve printed it to the console.
Similarly, you can also make use of the negative Indexing concept to remove the element from the reference from the end of the list. The index -1
represents the last element of the list, -2
represents the second last element and so on. This way, we have removed the third last element from the list and printed the returned value of the list.pop()
method.
Example 2. Removing the Last Element From the List using pop() method
As mentioned earlier, the index parameter of the list.pop()
method has the default value set to -1 which represents the index of the last element. Hence, you can always remove the last item of the list by using this method without passing any argument and this is illustrated in the following example.
1 2 3 4 5 6 7 |
#A Python List items = ["Pen", "Whiteboard", "Tablet", "Smartphone", "Cleaner"] #Removing the Last Item from the List removedItem = items.pop() print("Removed Item:",removedItem) |
Example 3. IndexError Simulation in case Index Argument is Invalid
In the following Python code, the last index of the defined list 6. But in the pop method, we’ve passed the index argument as 8.
1 2 3 4 5 |
numbers = [10, 23, 30, 42, 50, 55, 76] #Passing Index Out of the Range of the List #Will raise IndexError: pop index out of range numbers.pop(8) |
As no item at index 8 is present in the list, this method throws the IndexError: pop index out of range
.
Note. You can also remove an item from a Python List by the item itself and not by using its index. This can be done using the Python List remove() method. You simply have to provide the item to be removed itself as the argument to the remove method and the item will be removed from the list.
There’s also another way to delete a particular element from the list i.e. by using the del keyword. For example, if you want to delete the element at the 5th index of the list, you can use the del myList[5]
command. This will remove the item at 5th index from the Python list named myList
.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!