ReferenceSet methods

Python Set pop() Method

Short answer
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.

Quick facts
Syntaxmy_set.pop()
ParametersNone (a set's pop() takes no argument)
ReturnsThe removed element
RaisesKeyError if the set is empty
Time complexityO(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.

Python
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.

Python
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()

MethodWhich itemTakes an argument?Empty container
set.pop()ArbitraryNoKeyError
list.pop()The last oneOptional indexIndexError
list.pop(0)The first oneIndexIndexError
dict.popitem()The last inserted pairNoKeyError

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.

Take any one element
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.

Process every item once
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()
KeyError: 'pop from an empty set'

✓ 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)
TypeError: set.pop() takes no arguments (1 given)

✓ 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)
Not guaranteed. The result may look sorted for small integers but you must not rely on it.

✓ 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?

You cannot with pop(). Use remove() or discard() with the value you want to delete.

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 →