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.
Arguments
Parameters
| Name | Description |
|---|---|
| element | The element to remove. |
Result
Returns
None, modifies the set in place.
Run it
Try it
Run it and change it
permissions = {"read", "write"}
permissions.discard("write")
permissions.discard("admin")
print(sorted(permissions)) Output
Press Run to see what it prints.Worth knowing
- Safe removal: Use
discard()when you want idempotent element deletion without checkingif element in my_set:.