There are many times in coding when the programmer wants to remove the last element from the list. Python offers a built-in method in list methods to remove the last item from a list.
The python pop( ) method is an inbuilt function of Python. Pop( ) is used to remove the last item from a list and returns the removed object. You can also remove a list object at the specified index in the Python list.
In this article, we will go through few examples to understand the use of method pop() in Python List.
There are example codes along with the output for better understanding.
Table of Contents
Pop( ) Syntax:
The pop( ) method pops an item from a Python List. The basic syntax of the pop method is given by:
List_name.pop ( index )
Index: It specifies the index of the list elements that have to be removed from the list. It is an optional parameter.
When the given index is out of range, then it returns IndexError.
Example No.01
This is just a simple example of Python list Pop( ) method.
MyList1 = [ 2, 4, 6, 8, 10 ]
MyList1.pop()
print("MyList1 after popping last element:", MyList1)
Output

Example No.02
Following example returns those elements that will be removed from list.
MyList2 = [('Turkey', 'Switzerland'),'France', 20, 40, 60, 'Dubai' ]
print(MyList2.pop())
print(MyList2.pop())
print("New List after popping last 2 objects from list:", MyList2)
Output

Example No.03
This is how you can specify index in pop( ) method in Python list.
MyList3 = ['Apple', 'Mango', 'Orange', 'Strawberry', (1,3,5,7,9), 'Apricot' ]
print(MyList3.pop(0))
print(MyList3.pop(3))
print("New List after popping specified objects from list:", MyList3)
Output

Example No.04
This is an exception case when there is no element in Python List.
MyList4 = [ ]
MyList4.pop()
Output

Example No. 05
When you provide index that is out of range for Python List, it shows an error.
MyList5 = [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ]
print(MyList5.pop(9))
Output

Conclusion
This article has discussed the pop( ) method with two examples as well as exceptions. I hope this article is helpful in learning how to use the pop( ) method in Python List.