Python set.pop() Method

Definition
pop() removes one item from a set and hands it back to you. The set is changed in place and becomes one item smaller.
A set has no positions, so you cannot say which item to take: Python picks an arbitrary one. Calling pop() on an empty set raises KeyError.
set.pop() is an instance method of the built-in set type: call it on a set with dot notation, such as pool.pop(...). No import is needed.
Syntax
Call it with no arguments and use the returned item.
| Call | What it does |
|---|
{7}.pop() | Returns 7; the set becomes set(), because it had only one item. |
{1, 2, 3}.pop() | Removes and returns one of the three items. Which one is not defined, so two items stay in the set. |
set().pop() | Raises KeyError: there is nothing to pop from an empty set. |
Unlike remove() and discard(), pop() takes no argument. To take a chosen item use one of those; to take the smallest, use min(s) and then remove().
Method signatures
set.pop()
# returns: one arbitrary item, which is also removed from the set
# raises: KeyError if the set is empty
Parameters
This method takes no parameters: pop(0) raises TypeError, because a set has no positions.
Return Value
| Return Value | Description |
|---|
| the removed item | The item that was taken out of the set. Its type is whatever the item was: a number, a string, a tuple, and so on. |
Python Examples
Example 1 — Draw One Raffle Winner
Pop one ticket from the set and check what changed, without depending on which ticket it was.
tickets = {"T-101", "T-205", "T-318"}
original = set(tickets)
winner = tickets.pop()
print(winner in original)
print(winner in tickets)
print(len(tickets))
How It Works
- A snapshot first.
set(tickets) makes a separate copy, so you can still check the winner against all three tickets. - The winner was a real ticket. Whatever
pop() returns was in the original set, so the first test prints True. - The winner has left the set. The ticket was removed, so the second test prints
False and two tickets remain.
Example 2 — Process Every Task in a Worklist
Pop tasks until the set is empty, then see what popping an empty set does.
pending = {"email", "backup", "report"}
done = []
while pending:
task = pending.pop()
done.append(task)
print(len(done), len(pending))
print(sorted(done))
try:
pending.pop()
except KeyError:
print("nothing left to pop")
3 0
['backup', 'email', 'report']
nothing left to pop
How It Works
- An empty set is false.
while pending: keeps looping as long as the set has items and stops when it is empty. - Order is not promised. Each pass takes some task, so the examples only print the sorted list, which is the same on every run.
- Everything moved over. Three tasks ended up in
done and none remain in pending. - One pop too many. The set is empty now, so
pop() raises KeyError, which the except block catches.
Example 3 — Remove the Lowest Score on Purpose
Use min() and remove() when order matters, and pop() on a set with a single item.
scores = {82, 45, 97, 63}
lowest = min(scores)
scores.remove(lowest)
print(lowest, len(scores))
only = {"last"}
print(only.pop())
print(only)
How It Works
- Choose the item yourself.
min() finds the smallest score, 45, and remove() deletes exactly that one. - Three scores remain. 82, 97, and 63 are still in the set.
- One item leaves no doubt. With a single item,
pop() has only one choice and always returns "last". - The empty set prints as set(). There is no
{} for an empty set, because that is the literal for an empty dict.
Frequently Asked Questions
An arbitrary one. A set has no order, so you cannot choose or predict it, even if small numbers seem to come out in order on your machine.
It raises KeyError. Loop with while s: so you stop at the right time, or wrap the call in try/except KeyError.
Use min(s) or max(s) to find it, then s.remove(it). Or keep a sorted list if you need to take items in order repeatedly.
No. Unlike list.pop(i), set.pop() takes no arguments, because sets have no positions. Passing one raises TypeError.
Convert it to a sequence first, such as random.choice(list(s)). pop() is arbitrary, but it is not designed to be random.
Next: set.remove()
Delete an item from the set; raises KeyError if the item is missing.
remove() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful