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.
Returns
The removed element.
Try it
Run it and change it
items = {"alpha", "beta", "gamma"}
removed = items.pop()
print("Remaining count:", len(items))Worth knowing
- KeyError on empty sets: Always check
if my_set:before callingpop()to prevent exceptions.