pop() method in Python Lists can be used to pop/ remove the last element from the list.
The pop() method in Python Lists returns an ‘integer‘ as a value, denoting the element that was popped and modifies the original list itself.
Syntax
list.pop()
l = [1, 2, 4, 5, 10]
print(l.pop()) # returns 10
print(l) # returns modified list
O/P
10
[1, 2, 4, 5]
Please note that if you use pop() method with an empty list, then it will result in IndexError.
l = []
l.pop()
O/P
Traceback (most recent call last):
File “”, line 2, in
IndexError: pop from empty list
You can also use the Python del statement to remove items from the list.
That’s all here.
