set.discard()
set.discard() is a Python method in the Set Methods category. Removes element x if present (does nothing if not found). The syntax is set.discard(x). Parameters: x — element to remove. Returns: None. A typical example: s = {1, 2, 3}
s.discard(2) # s = {1, 3}
s.discard(4) # s = {1, 3} (no error). A close sibling is set.add(), which adds an element to the set if not already present. A close sibling is set.remove(), which removes element x; raises KeyError if not found. A close sibling is set.pop(), which removes and returns an arbitrary element. Raises KeyError if empty. A close sibling is set.clear(), which removes all elements from the set. More about this category: Set methods for collection operations — union, intersection, difference, and more. For a broader walkthrough, read the Set Methods guide. Related Set Methods entries: set.add(), set.remove(), set.pop(), set.clear(), set.union(), set.intersection().