Set Methods
remove()
Remove an element from the set; raises KeyError if not found.
set.remove(element)
set.remove() removes element from the set in place.
If the element is not found, it raises a KeyError exception.
Parameters
| Name | Description |
|---|---|
| element | The element to remove. |
Returns
None — modifies the set in place.
Try it
Run it and change it
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(sorted(fruits))Worth knowing
- remove() vs discard(): Use
remove()when missing elements should be treated as an error; usediscard()for silent deletion.