Set Methods
pop()
Remove and return an arbitrary element from the set.
set.pop()
set.pop() removes and returns a single element from the set.
Because sets are unordered, you cannot predict which element will be popped. Raises KeyError if the set is empty.
Result
Returns
The removed element.
Run it
Try it
Run it and change it
items = {"alpha", "beta", "gamma"}
removed = items.pop()
print("Remaining count:", len(items)) Output
Press Run to see what it prints.Worth knowing
- KeyError on empty sets: Always check
if my_set:before callingpop()to prevent exceptions.