In this Python tutorial, you will learn how to remove and return the last element from a list using the pop() method. You will also learn what happens when pop() is called on an empty list and how to avoid the resulting error.
Remove the Last Element from a Python List Using pop()
The Python list.pop() method removes the last element from a list and returns the removed element. Because the method changes the list in place, the original list becomes one element shorter.
For example, if a list contains ['apple', 'banana', 'cherry'], calling pop() returns 'cherry' and leaves the list as ['apple', 'banana'].
Calling pop() on an empty list raises an IndexError because there is no element available to remove.
Syntax to Pop the Last Item from a Python List
The syntax to call the pop() method on a list named myList is:
myList.pop()
The method returns the removed element. You can assign this return value to a variable when you need to use the element later.
Python List pop() Examples
1. Pop the Last Item from a Non-Empty List
In the following program, we initialize a list with three strings and call pop() without an argument. The method removes and returns the final element.
main.py
#initialize list
myList = ['apple', 'banana', 'cherry']
#pop item from list
item = myList.pop()
#print popped item
print(f'popped item is : {item}')
#print list
print(f'resulting list : {myList}')
Output
popped item is : cherry
resulting list : ['apple', 'banana']
The returned value is stored in item. The original list is modified and no longer contains 'cherry'.
Reference tutorials for the above program
2. Pop an Item from an Empty Python List
In this example, we create an empty list and call pop(). Since the list contains no elements, Python raises an exception.
main.py
#empty list
myList = []
#pop item from list
item = myList.pop()
#print popped item
print('popped item is :', item)
#print list
print(myList)
Output
Traceback (most recent call last):
File "main.py", line 5, in <module>
item = aList.pop()
IndexError: pop from empty list
Since the list is empty, the pop() method raises IndexError: pop from empty list.
Reference tutorials for the above program
3. Avoid IndexError Before Calling list.pop()
When a list may be empty, check its truth value before calling pop(). A non-empty list evaluates to True, while an empty list evaluates to False.
myList = []
if myList:
item = myList.pop()
print('popped item is :', item)
else:
print('Cannot pop an item because the list is empty.')
Output
Cannot pop an item because the list is empty.
This condition prevents the IndexError and allows the program to handle an empty list explicitly.
How list.pop() Changes the Original List
The pop() method performs two operations:
- It removes the last element from the original list.
- It returns the removed element to the caller.
This differs from reading the last element with myList[-1]. Indexing returns the element without removing it, while pop() returns the element and changes the list.
numbers = [10, 20, 30]
last_value = numbers[-1]
print(last_value)
print(numbers)
popped_value = numbers.pop()
print(popped_value)
print(numbers)
Output
30
[10, 20, 30]
30
[10, 20]
Python list.pop() Questions
What does list.pop() return in Python?
The method returns the element it removes. When called without an argument, it removes and returns the last element.
Does pop() modify the original Python list?
Yes. The removed element is deleted from the original list, reducing its length by one.
What error occurs when pop() is called on an empty list?
Python raises IndexError with the message pop from empty list.
How can I remove the last list item without using pop()?
You can use del myList[-1] to remove the last item, but del does not return the removed value. Use pop() when you need both removal and the removed element.
Summary of Popping the Last Element from a List
In this Python Tutorial, we learned how to remove and return the last element of a list using list.pop(). We also examined how the method modifies the original list, why an empty list raises IndexError, and how to check a list before popping an element.
TutorialKart.com