set.remove() Remove element
Last updated: 2026-09-22
set.remove() Remove element
set.remove(elem) removes elem; raises KeyError if missing.
| Category | Set Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
set.remove(elem)
⚙️ Parameters
| elem Required | The element to remove; raises KeyError if missing. |
|---|
Returns:None (removes elem in place).
Mutates the original:Yes (in place)
💥 Raises
KeyError— Raised when elem is not in the set.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
{1, 3}
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between remove() and discard()?
Aremove() raises a KeyError when the element does not exist; discard() silently ignores it.
QWhen should I use remove()?
AWhen the element "must exist" and its absence indicates a data problem, use remove() so the exception immediately exposes the bug.
QWhat does it return?
ANone; it deletes in place.