set.intersection_update()
The in-place intersection — keep the overlap with the given iterable(s). Everything not shared is removed.
Demo
intersection_update mutates the set in place. Real Python returns None; the demo shows the RESULTING state so the effect is visible. Every element that is NOT in the iterable is removed from the set. When the other iterable is empty, the set becomes empty. The iterable can be any iterable, not just another set.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| *others | iterable | no (()) | Zero or more iterables. The set is narrowed to elements found in ALL of them. With zero args the set is unchanged. |
Return value
None — Returns None. The set is mutated in place — keeps only elements that appear in the set AND in every given iterable. Equivalent to `s &= set(other1) & set(other2) & ...` but takes any iterables, not just sets.
Common patterns
active.intersection_update(allowed_ids)
candidates.intersection_update(available, in_budget, matches_criteria)
s &= other_set # requires a set s.intersection_update(iterable) # any iterable
candidates = set(all_users) candidates.intersection_update(paying_users) candidates.intersection_update(active_users)
Examples
Pitfalls
s = s.intersection_update(other)
s.intersection_update(other)
{"a", "b"}.intersection_update([])
if other: s.intersection_update(other)
set.intersection_update([1,2], [3,4]) # each restricts
s.intersection_update(set(iterable1) | set(iterable2))
s = {"hi", "h"} s.intersection_update("hi")
s.intersection_update(["hi"])
When to use
- Narrowing a set to an allowlist
- Progressive filtering through multiple constraints
- Building a "kept" state from a candidate pool
- In-place refinement without allocating a new set
- You need a new set — use `&` or `intersection()` for a pure result
- You want elements in either — use `|` or union
- Empty iterable should NOT clear the set → guard first
- Non-hashable elements → filter or convert to tuples first
Notes
FAQ
For a set on the right, they are equivalent: `s &= t` calls `s.intersection_update(t)`. But intersection_update accepts ANY iterable (list, string, generator) and multiple iterables in one call. `&=` requires a set on the right.