set.remove
Remove an item from the set.
Python 3.13
set.remove(elem)- elem
- The item to remove.Hashable
Return value
Nothing (None); the set is changed in place. Raises KeyError if the item is missing.
Examples
Removing an existing item
Python 3.13
s = {1, 2, 3}
s.remove(3)
print(s){1, 2}Error for a missing item
Python 3.13
s = {1, 2}
try:
s.remove(5)
except KeyError as error:
print(error)5
Details
If the item may be missing, use set.discard, which raises no error.
