set.clear()

Empty the set in place — the SAME object becomes empty. All references see the change.

Set methodPython 2.6+Live demo
Common call
seen.clear()
Returns
None — set is empty afterwards
Replaces
a loop of `s.discard(x)` calls, or `s -= s.copy()`
Watch out
different from `s = set()` — clear mutates every reference; `= set()` rebinds only the local name
set.clear()
None

Demo

Live evaluation
Try:
Inputs
setsetcomma-separated elements
Output
{'a', 'b', 'c'}.clear()
None

clear empties the set in place. Python actually returns None; the demo shows the resulting state (always empty) so you can see the effect. The subtle and important thing NOT visible here: any other name pointing at the same set sees the change too. That is the difference from `s = set()`, which only rebinds the local name.

Common patterns

Reset a "seen" set between passes
Every consumer holding a reference to `seen` sees the empty state immediately.
def run(passes):
    for _ in range(passes):
        seen.clear()
        process(seen)
Drain then repopulate
Preserve identity of a shared set while replacing its contents.
shared_set.clear()
shared_set.update(new_values)
Empty a nested set without breaking outer references
clear on the inner set keeps the outer structure intact.
user["blocked"].clear()   # blocked set empties, user still points at it

Examples

1. Basic
s = {1, 2, 3} s.clear() s
Returns
set()
2. Already empty
set().clear()
Returns
None # no error
3. Returns None
{"a", "b"}.clear()
Returns
None
4. Affects all references
s = {1, 2} t = s s.clear() t
Returns
set() # t sees the change too
5. Assignment does NOT
s = {1, 2} t = s s = set() t
Returns
{1, 2} # t still points at the original

Pitfalls

1. clear() is NOT the same as `s = set()`
clear mutates the existing set — every reference sees it become empty. `s = set()` creates a NEW empty set and rebinds the local name; every other reference still points at the original (which is unchanged).
Rebind misses aliases
shared = {1, 2, 3}
local = shared
shared = set()
local
{1, 2, 3} # aliases still see the old data
clear reaches everyone
shared = {1, 2, 3}
local = shared
shared.clear()
local
set() # every alias sees empty
2. The `s = s.clear()` bug
clear() returns None. Assigning its result back sets your variable to None — the same class of bug as add, discard, and remove.
Now s is None
s = {1, 2}
s = s.clear()
print(s)
None
Just clear
s.clear()   # mutate, keep name
set()
3. Clearing while iterating raises
Modifying the set during iteration is a RuntimeError, and clear is a modification.
Runtime error
for x in s:
    if predicate(x):
        s.clear()
RuntimeError: Set changed size during iteration
Iterate a snapshot
for x in list(s):
    ...
safe
4. frozenset has NO clear() — it is immutable
frozenset is the read-only sibling. Calling clear on a frozenset raises AttributeError. To "clear" a frozenset, replace it with an empty frozenset.
AttributeError
frozenset([1, 2]).clear()
AttributeError: 'frozenset' object has no attribute 'clear'
Rebind
fs = frozenset()
a fresh empty frozenset

When to use

Use it
  • Resetting a shared / module-level / cache set without rebinding
  • Preparing a container between test runs or workflow stages
  • Emptying a nested set without breaking outer references
  • Any "empty this and keep the identity" requirement
Reach for something else
  • You want a fresh, independent empty set → `s = set()`
  • You have a specific element to remove → set.discard / set.remove
  • Iterating and removing selectively → build a new set with a comprehension
  • frozenset — no clear method exists

Notes

Complexity
O(n) — walks and releases every entry
Return
None; the set is mutated in place
CPython impl
Objects/setobject.c :: set_clear
Memory
Frees entries; the hash table itself may keep some capacity
Thread-safe
Not safe under concurrent iteration or mutation

FAQ

clear() mutates the SAME set — every reference to it sees it become empty. `s = set()` creates a NEW empty set and rebinds just the local name; every other reference still points at the original.

History

2.6
clear() added to set.