Python List pop() Method
list.pop(index=-1) removes the item at index (the last item by default) and returns it; it raises IndexError for an empty list or an invalid index.pop() is the method to use when you want to take an item out of a list and use it. That makes it perfect for stacks (last in, first out), for processing a to-do list one task at a time or for grabbing an item at a known position.
If you only know the value you want to delete, use list.remove(). If you do not need the removed item, del my_list[i] does the same job without returning anything.
| Syntax | my_list.pop() or my_list.pop(index) |
|---|---|
| Parameters | index (optional): position to remove, default -1 (the last item) |
| Returns | The removed item |
| Raises | IndexError if the list is empty or the index is out of range |
| Time complexity | O(1) for the last item, O(n) for other positions |
How list.pop() removes and returns an item
Without an argument pop() takes the last element. With an index it takes that element and shifts the later ones left. Negative indexes count from the end, just like in slicing.
stack = ["a", "b", "c", "d"]
print(stack.pop())
print(stack.pop(0))
print(stack)Using pop() as a stack
A stack adds with append() and removes with pop(), both at the end, and both are fast. This pattern shows up in undo features, bracket matching and depth-first search. Popping from the front with pop(0) works too, but it is O(n) because every other item shifts; for large queues use collections.deque.popleft().
pop() compared with remove(), del and clear()
All of these delete, but they differ in how you choose the item and what you get back.
| Tool | Chooses item by | Gives back | Error when invalid |
|---|---|---|---|
pop() / pop(i) | Index (default last) | The removed item | IndexError |
remove(v) | Value | None | ValueError |
del a[i] | Index or slice | Nothing | IndexError |
clear() | Removes all | None | None |
Examples you can run
Press Run, then edit the code and run it again.
Pop the last and a chosen item
The returned value can be stored in a variable.
queue = ["Asha", "Dev", "Meera", "Ravi"]
last = queue.pop()
second = queue.pop(1)
print(last, second)
print(queue)Expected outputRavi Dev
['Asha', 'Meera']
Process tasks until the list is empty
Checking the list in the while condition prevents an IndexError.
tasks = ["write", "test", "deploy"]
while tasks:
current = tasks.pop()
print("doing", current)
print(tasks)Expected outputdoing deploy
doing test
doing write
[]
Pop with a negative index
pop(-2) removes the second item from the end.
nums = [10, 20, 30, 40]
print(nums.pop(-2))
print(nums)Expected output30
[10, 20, 40]
Common mistakes
Popping from an empty list
✗ Wrong
stack = []
stack.pop()✓ Right
stack = []
if stack:
stack.pop()
print(stack)Check if stack: before popping. See IndexError for more causes.
Index out of range
✗ Wrong
nums = [1, 2, 3]
nums.pop(5)✓ Right
nums = [1, 2, 3]
print(nums.pop(2))Use an index smaller than len(nums), or negative down to -len(nums).
Passing a value instead of an index
✗ Wrong
names = ["Asha", "Dev"]
names.pop("Dev")✓ Right
names = ["Asha", "Dev"]
names.remove("Dev")
print(names)pop() takes a position. To delete by value use remove("Dev").
Python List pop() Method: questions people ask
What does pop() do in Python?
It removes an item from a list and returns it. With no argument it removes the last item; with an index it removes the item at that position.
What is the difference between pop() and remove()?
pop() removes by index and returns the item, while remove() removes the first item equal to a value and returns None.
How do I pop the first element of a list?
Use my_list.pop(0). It works but is O(n), so for big queues prefer collections.deque and popleft().
Keep learning
Step-by-step lessons:
Python ListsPython List Methods
Related:
Python List remove() MethodPython List append() MethodPython List clear() MethodPython List insert() Method
More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List remove() MethodPython List clear() MethodPython List index() MethodPython List count() MethodPython List sort() Method See all →