Python Set pop() Method
The pop() method removes and returns a random element from the set.
Syntax
set.pop()
Parameters
No parameters.
Return Value
Returns the element that was removed. It raises KeyError if the set is empty.
Example
fruits = {"apple", "banana", "mango", "orange"}
removed = fruits.pop()
print(removed) # e.g. Output: banana
print(fruits) # Remaining Elements
Python sets are unordered, so the elements may appear in a different order each time you print them.
Empty Set Behavior
If you call the pop() method on an empty set, Python raises a KeyError.
For example:
empty = set()
empty.pop() # Raises KeyError