Python 3 reference · Set Methods
Python Set remove() Method
Remove an element from the set. Raises KeyError if it isn't present; use discard() to avoid the error.
Python 3 Since 3.0
Syntax
set.remove(elem)
Parameters
| Parameter | Description |
|---|---|
elem |
The element to remove. |
Return value
None.
Examples
s = {"a", "b"}
s.remove("a")
print(s)
Output
{'b'}s = {"a"}
try:
s.remove("z")
except KeyError as e:
print("KeyError:", e)
Output
KeyError: 'z'
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.pop()· Remove and return an arbitrary element of the set. Raises KeyError if the set is empty.- Python Sets · tutorial