ReferenceSet methods

Python Set intersection() Method

Short answer
a.intersection(b) returns a new set with only the elements that appear in both a and b. The original sets are unchanged.

Use intersection to find what groups have in common: students enrolled in both Python and Maths, products that appear in two shops, or tags shared by two posts. It is one of the main set operations.

The result is empty (set()) when the sets share nothing. If you want to ignore the shared items instead, look at difference().

Quick facts
Syntaxa.intersection(*others) or a & b
ParametersZero or more iterables
ReturnsA new set
Changes the original?No (intersection_update() does)
Time complexityAbout O(min(len(a), len(b))) for two sets

How set.intersection() works

Python walks the smaller set and keeps each element that is also in the other, which is fast because membership tests on sets are O(1) on average.

Python
python_class = {"Asha", "Ravi", "Meera"}
maths_class = {"Ravi", "Meera", "Kabir"}
print(sorted(python_class.intersection(maths_class)))
print(sorted(python_class & maths_class))

Intersection of more than two sets

Pass several arguments and only elements present in all of them survive. With no argument at all, a.intersection() returns a copy of a.

Python
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 9}
print(sorted(a.intersection(b, c)))

intersection() vs intersection_update() vs &

FormAcceptsResultModifies the original?
a.intersection(b)Any iterableNew setNo
a & bSets onlyNew setNo
a.intersection_update(b)Any iterableNoneYes, keeps only common items
a &= bSets onlyUpdates aYes
a.isdisjoint(b)Any iterableTrue or FalseNo

Examples you can run

Press Run, then edit the code and run it again.

Students in both classes

Only the names present in both sets are kept.

Students in both classes
python_class = {"Asha", "Ravi", "Meera"}
maths_class = {"Ravi", "Meera", "Kabir"}
both = python_class.intersection(maths_class)
print(sorted(both))
print(len(both))
Expected output['Meera', 'Ravi']
2

No overlap gives an empty set

Using a list as the argument works too, and no overlap yields set().

No overlap gives an empty set
cart = {"pen", "book"}
wishlist = ["laptop", "mouse"]
common = cart.intersection(wishlist)
print(common)
print(bool(common))
Expected outputset()
False

Common mistakes

Expecting the original to shrink

✗ Wrong

a = {1, 2, 3}
a.intersection({2, 3})
print(a)
{1, 2, 3} (the result was discarded)

✓ Right

a = {1, 2, 3}
a = a.intersection({2, 3})
print(sorted(a))

Assign the result, or use intersection_update() to modify a in place.

Using & with a list

✗ Wrong

a = {1, 2, 3}
print(a & [2, 3])
TypeError: unsupported operand type(s) for &: 'set' and 'list'

✓ Right

a = {1, 2, 3}
print(sorted(a & set([2, 3])))

Use the method, or wrap the list with set().

Python Set intersection() Method: questions people ask

How do I find common elements between two lists in Python?

Convert both to sets and intersect them: set(a) & set(b). The result is unordered, so use sorted() if you need an order.

What is the difference between intersection() and the & operator?

They give the same result for sets, but the method accepts any iterable and several arguments while & needs sets on both sides.

What does intersection() return if there are no common elements?

It returns an empty set, printed as set(), not None and not an error.

Keep learning

Step-by-step lessons:
Python Set OperationsPython Sets

Related:
Python Set union() MethodPython Set difference() MethodPython Set issubset() MethodPython Set symmetric_difference() Method

More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set difference() MethodPython Set symmetric_difference() MethodPython Set update() Method See all →