Python set.pop() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set 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.

python
item = s.pop()
CallWhat 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
python
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 ValueDescription
the removed itemThe 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.

python
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

  1. A snapshot first. set(tickets) makes a separate copy, so you can still check the winner against all three tickets.
  2. The winner was a real ticket. Whatever pop() returns was in the original set, so the first test prints True.
  3. 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.

python
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")

How It Works

  1. An empty set is false. while pending: keeps looping as long as the set has items and stops when it is empty.
  2. Order is not promised. Each pass takes some task, so the examples only print the sorted list, which is the same on every run.
  3. Everything moved over. Three tasks ended up in done and none remain in pending.
  4. 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.

python
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

  1. Choose the item yourself. min() finds the smallest score, 45, and remove() deletes exactly that one.
  2. Three scores remain. 82, 97, and 63 are still in the set.
  3. One item leaves no doubt. With a single item, pop() has only one choice and always returns "last".
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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