Python Set pop() Method
set.pop() removes one arbitrary element from the set and returns it. On an empty set it raises KeyError.Sets have no order and no index, so there is no "first" or "last" element to pop. pop() simply hands you whichever element is convenient for Python, which is fine for work-queue style loops where every item must be processed once and the order does not matter.
If you need to remove a specific value, use remove() or discard(). If you need a predictable order, sort the elements or use a list.
| Syntax | my_set.pop() |
|---|---|
| Parameters | None (a set's pop() takes no argument) |
| Returns | The removed element |
| Raises | KeyError if the set is empty |
| Time complexity | O(1) on average |
How set.pop() chooses an element
The Python documentation only promises that pop() removes and returns an arbitrary element. Which one you get depends on the internal hash table, and for strings it can differ between runs because string hashing is randomised. Never write code that relies on a particular element coming out.
last = {"only"}
print(last.pop())
print(len(last))Draining a set with pop()
A common pattern is to process every element exactly once by popping until the set is empty. Testing the set itself works because an empty set is falsy.
tasks = {"email", "backup", "report"}
done = []
while tasks:
done.append(tasks.pop())
print(sorted(done))
print(len(tasks))set.pop() vs list.pop() and dict.popitem()
| Method | Which item | Takes an argument? | Empty container |
|---|---|---|---|
set.pop() | Arbitrary | No | KeyError |
list.pop() | The last one | Optional index | IndexError |
list.pop(0) | The first one | Index | IndexError |
dict.popitem() | The last inserted pair | No | KeyError |
Examples you can run
Press Run, then edit the code and run it again.
Take any one element
With a one-element set the result is certain, which makes the return value easy to see.
winners = {"Asha"}
name = winners.pop()
print(name)
print(winners)Expected outputAsha
set()
Process every item once
We pop until the set is empty and sum the values. Addition does not depend on order, so the output is stable.
scores = {10, 20, 30, 40}
total = 0
while scores:
total += scores.pop()
print(total)
print(scores)Expected output100
set()
Common mistakes
Popping from an empty set
✗ Wrong
cart = set()
cart.pop()✓ Right
cart = set()
if cart:
cart.pop()
print("nothing to pop")Check that the set is not empty first, for example if cart:.
Passing an element to pop()
✗ Wrong
s = {1, 2, 3}
s.pop(2)✓ Right
s = {1, 2, 3}
s.discard(2)
print(sorted(s))pop() takes no argument. To remove a specific element use remove(2) or discard(2).
Assuming pop() returns the smallest value
✗ Wrong
s = {3, 1, 2}
print(s.pop() == 1)✓ Right
s = {3, 1, 2}
smallest = min(s)
s.remove(smallest)
print(smallest, sorted(s))Use min(s) to find the smallest, then remove() it.
Python Set pop() Method: questions people ask
Does set.pop() remove a random element?
It removes an arbitrary element, not a truly random one. Python makes no promise about which, so do not depend on it. Use random.choice on a list if you need real randomness.
How do I pop a specific element from a set?
What does pop() return on an empty set?
It does not return anything. It raises KeyError, so check that the set is not empty before calling it.
Keep learning
Step-by-step lessons:
Python SetsPython Truthy and Falsy Values
Related:
Python Set remove() MethodPython Set discard() MethodPython Set clear() MethodPython Lists
More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set union() MethodPython Set intersection() MethodPython Set difference() MethodPython Set symmetric_difference() MethodPython Set update() Method See all →