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
s = {"only"}
print(s.pop(), s)
Output
only set()
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
set.discard()· Remove an element if it is present; do nothing if it isn't. The no-error version of remove().set.remove()· Remove an element from the set. Raises KeyError if it isn't present; use discard() to avoid the error.set.clear()· Remove every element from the set in place, leaving an empty set. Other references to the set see the change.- Python Sets · tutorial