Set Methods
discard()
Remove an element from the set if it is present without raising an error.
set.discard(element)
set.discard() removes element from the set if found.
Unlike set.remove(), if element is not in the set, discard() does nothing and does not raise a KeyError.
Parameters
| Name | Description |
|---|---|
| element | The element to remove. |
Returns
None — modifies the set in place.
Try it
Run it and change it
permissions = {"read", "write"}
permissions.discard("write")
permissions.discard("admin")
print(sorted(permissions))Worth knowing
- Safe removal: Use
discard()when you want idempotent element deletion without checkingif element in my_set:.