Python Glossary Русский

all Python 3Sets (set)function

set.discard

removal

Remove an element if present; a missing element is not an error (unlike remove).

Syntax

s.discard(elem)

The silence of discard is exactly what trips people up: a typo or a mismatched type (the string '2' instead of the number 2) removes nothing and reports nothing. Reach for discard when a missing element is a normal outcome; if the element is supposed to be there, remove will surface the logic error immediately.

Examples

s = {1, 2, 3}
s.discard(2)
s.discard(9)
print(sorted(s))   # → [1, 3]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy