Python 3 reference · Set Methods

Python Set pop() Method

Remove and return an arbitrary element of the set. Raises KeyError if the set is empty.

Python 3 Since 3.0

Syntax

set.pop()

Return value

The removed element.

Examples

Take an elementTry it yourself
s = {"only"}
print(s.pop(), s)

Output

only set()
Process every element (order not guaranteed)Try it yourself
pending = {3, 1, 2}
done = []
while pending:
    done.append(pending.pop())
print(sorted(done))

Output

[1, 2, 3]

Sets are unordered, so you can't choose which element pop() returns.

Version history

VersionChange
Python 3.0Added