set.intersection()

Find elements common to two or more collections — a fresh set, no mutation.

Set methodPython 2.6+Live demo
Common call
s1 & s2
Returns
a NEW set — self and others untouched
Replaces
a filter + membership-check loop
Watch out
others can be any iterable — but the `&` operator requires sets on both sides
set.intersection(*others)
set

Demo

Live evaluation
Try:
Inputs
asetfirst set (comma-separated)
bsetsecond set (comma-separated)
Output
{'1', '2', '3', '4'}.intersection({'3', '4', '5', '6'})
TypeError: 'object' object is not iterable

intersection returns a NEW set — self is untouched. An element is kept only if it appears in every input. The demo input arrives as CSV; duplicates within one input collapse on the way in (that is what sets do). Order shown is not meaningful — Python sets are unordered.

Parameters

NameTypeRequiredDescription
*othersiterableno (())Zero or more iterables. Elements are kept only if they appear in every one. Any type: set, list, tuple, generator, string, dict (keys).

Return value

setA NEW set containing only elements that appear in self AND in every iterable in others. Neither self nor others are modified.

Common patterns

Common tags across posts
The operator form reads like "a AND b".
shared = post_a.tags & post_b.tags
Intersect many
intersection takes variadic args; each may be any iterable.
always = set.intersection(*(user.roles for user in users))
Test disjointness
Empty intersection means the two sets share nothing — but isdisjoint is more direct.
if not a & b:
    ...     # disjoint
if a.isdisjoint(b):
    ...     # clearer

Examples

1. Partial overlap
{1, 2, 3} & {2, 3, 4}
Returns
{2, 3}
2. Iterable other
{1, 2, 3}.intersection([2, 3, 4])
Returns
{2, 3}
3. Multiple others
{1, 2, 3}.intersection({2, 3}, [3, 4])
Returns
{3}
4. Disjoint gives empty
{1, 2} & {3, 4}
Returns
set()
5. Zero others is self
{1, 2, 3}.intersection()
Returns
{1, 2, 3}

Pitfalls

1. The `&` operator requires sets on both sides
intersection() accepts any iterable. The `&` operator does NOT — it needs a set on both sides.
Type error
{1, 2, 3} & [2, 3]
TypeError: unsupported operand type(s) for &: 'set' and 'list'
Method form
{1, 2, 3}.intersection([2, 3])
{2, 3}
2. intersection() is NOT intersection_update() — it returns a new set
intersection leaves both inputs alone and returns a fresh set. intersection_update mutates the left one and returns None. Same class of confusion as sort vs sorted.
Original untouched
a = {1, 2, 3}
a.intersection({2, 3})
a
{1, 2, 3} # nothing removed
Two options
a = a & {2, 3}                # new set, replace name
# or
a.intersection_update({2, 3})   # mutate in place
{2, 3}
3. String iterables explode into characters
Same footgun as union — a string passed as an "other" is iterated as characters, so nothing bigger than one char can match.
Only chars match
{"Ann", "Bob"}.intersection("BobAnn")
set() # no whole-name matches
Wrap it
{"Ann", "Bob"}.intersection({"Bob"})
{"Bob"}
4. Zero others returns a COPY of self
`s.intersection()` with no arguments is treated as intersecting with nothing to require — every element in self passes. The result is a copy, not self itself.
Assumed identity
s = {1, 2, 3}
s.intersection() is s
False # equal but not the same object
Check equality
s.intersection() == s
True

When to use

Use it
  • Elements common to two or more collections
  • Filter-by-membership without a manual loop
  • Access-control style checks — required roles vs granted roles
  • Chaining with other pure set operations (|, &, -)
Reach for something else
  • You want to mutate in place → intersection_update or &=
  • Preserving order → use dict.fromkeys and filter
  • Just checking "is there any overlap?" → isdisjoint (clearer, may short-circuit)
  • Unhashable elements → use a list-comprehension with the `in` operator

Notes

Complexity
O(min(|a|, |b|)) for two-set intersection; Python iterates the smaller and probes the larger
Return
A new set — same type as self (`set` or `frozenset`)
CPython impl
Objects/setobject.c :: set_intersection
Memory
Allocates a new set sized for the overlap
Thread-safe
Safe against reads; not safe under concurrent writes to the input sets

FAQ

They compute the same thing, but intersection() accepts ANY iterable (list, tuple, generator, string). The `&` operator requires both sides to be sets. Method: flexible. Operator: strict.

History

2.3
set type added; intersection available as `&` operator.
2.6
intersection() method accepts multiple iterable arguments (variadic).