Python sets have three removal methods: set.remove(x) deletes an element and raises KeyError if it is missing, set.discard(x) deletes it silently if present, and set.pop() removes and returns an arbitrary element. To empty a set completely, call set.clear().
Because sets are unordered and contain unique hashable items, you never remove by index, only by value. The sections below show each method, how to remove many items at once, and how to filter a set safely.
remove() when the element must existdiscard() for a safe removalpop() and clear()remove() when the element must existremove(x) deletes x from the set. If x is not there, Python raises KeyError, which is useful when a missing element signals a bug.
The method returns None and changes the set in place.
discard() for a safe removaldiscard(x) does the same job but does nothing when x is absent, so you do not need a try block or a membership check.
In most application code discard() is the better default, and remove() is for cases where a missing item is an error you want to see.
pop() and clear()pop() removes one element and returns it, but you cannot choose which one. The result is arbitrary, so do not depend on it being the smallest or the first inserted. It raises KeyError on an empty set.
Note that an empty set prints as set(), not {}, because {} creates an empty dictionary.
Set operations remove many items in a single expression. Both forms below change the set in place.
difference_update() accepts any iterable, such as a list. The -= operator needs a set on the right side. If you want a new set and keep the original, use letters - {"b", "d"} or letters.difference({"b", "d"}) instead.
Never loop over a set and remove from it at the same time. Python raises RuntimeError: Set changed size during iteration. Build a new set with a comprehension, or collect the unwanted items first.
Iterating over set(numbers) makes a copy, so changing the original inside the loop is safe. If other variables must keep pointing at the same set object, prefer the in-place forms.
Set elements must be hashable, so you can only remove values that could have been in the set in the first place. Calling discard([1, 2]) raises TypeError: unhashable type: 'list', even with discard(). Use tuples or frozensets when you need compound members.
If you want to know whether anything was removed, check membership first or compare lengths:
| Method | Needs the element to exist | Returns | Typical use |
|---|---|---|---|
remove(x) | Yes, else KeyError | None | Missing item is a bug |
discard(x) | No | None | Safe removal |
pop() | Set must not be empty | The removed item | Process items one by one |
clear() | No | None | Empty the set |
-= or difference_update() | No | None | Remove many items |
Each of these removes an element in O(1) average time, except difference_update(), which is proportional to the size of the other collection.
remove() and discard() on a Python set?Both delete an element. remove() raises KeyError when the element is absent, while discard() ignores that case.
set.pop() remove?An arbitrary one. Sets have no defined order, so you cannot rely on it removing the smallest or the oldest item.
You cannot, because sets are not indexed. Remove by value, or convert to a sorted list first if you need positional access.
RuntimeError?Changing the size of a set while iterating over it is not allowed. Iterate over a copy with set(my_set), or build a new set with a comprehension.
Use discard() for safe removal, remove() when a missing item should fail loudly, pop() to take any element, and clear() to empty the set. Remove several items with -= or difference_update(), and filter by condition with a set comprehension or by looping over a copy.