CKTIS / Python
set.discard
Removes a member if it is present.
Version: Python 3.14; repository image, production runtime not attested
Syntax
s.discard(element, /)Removes a member if it is present.
A missing member is silently ignored, making repeated removal safe.
Discarding 99 leaves {2, 3} intact. Discarding 3 then removes it in place. Both calls return None, so use a membership test if the caller needs to know whether the set actually contained the element. Unlike remove, a missing member does not raise KeyError.
s = {1}
assert s.discard(9) is None and s.discard(1) is None and not s
Examples
s = {2, 3}
print(s.discard(99))
s.discard(3)
print(sorted(s))
Result
None [2]
Discarding 99 leaves {2, 3} intact. Discarding 3 then removes it in place. Both calls return None, so use a membership test if the caller needs to know whether the set actually contained the element. Unlike remove, a missing member does not raise KeyError.