Python Set intersection() Method

The set.intersection() method returns a new set containing only the values that appear in both sets.

Use it when you want to find common items between sets without changing the original set.

a = {1, 2, 3}
b = {2, 3, 4}

result = a.intersection(b)
print(result)
# Output: {2, 3}

Use intersection() when you want only the items that appear in both sets.

ab1234
a.intersection(b) shades only the overlap: values found in both sets.

What the intersection() method does #

intersection() is a set method that compares one set with another set or iterable and keeps only the shared values.

Key points:

  • It returns a new set with items that exist in both sets
  • It does not change the original set
  • It works with values that can be stored in a set
  • It is useful for finding shared values

If you are new to sets, see Python sets explained.

Basic syntax #

set1.intersection(set2)

You can also pass more than one argument:

set1.intersection(set2, set3)

Important notes:

  • The result is always a new set
  • If there are no shared items, the result is an empty set
  • You can pass sets or other iterables as arguments

Example:

a = {1, 2, 3, 4}
b = {3, 4, 5}

result = a.intersection(b)
print(result)

Output:

{3, 4}

Return value #

intersection() returns a set.

That returned set contains only the elements that are present in all inputs.

a = {1, 2, 3}
b = {2, 3, 4}

result = a.intersection(b)

print(result)
print(type(result))

Output:

{2, 3}
<class 'set'>

Remember:

  • The return value is a set, not a list
  • The order is not guaranteed because sets are unordered

Simple example #

Here is a basic example with two sets that share some values:

colors1 = {"red", "blue", "green"}
colors2 = {"green", "yellow", "blue"}

common_colors = colors1.intersection(colors2)

print(common_colors)

Output:

{'blue', 'green'}

What happens here:

  • colors1 contains three color names
  • colors2 contains three color names
  • intersection() keeps only the values found in both sets
  • The result is a new set stored in common_colors

Using intersection() with multiple sets #

You can pass more than two sets or iterables to intersection().

Only the values found in all inputs will be kept.

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 6}

result = a.intersection(b, c)
print(result)

Output:

{3, 4}

This is useful when you want to check which items are shared across many groups.

You can also pass other iterables:

a = {1, 2, 3, 4}

result = a.intersection([2, 4, 6], (1, 2, 4))
print(result)

Output:

{2, 4}

intersection() vs intersection_update() #

These two methods are similar, but they work differently.

intersection() #

  • Returns a new set
  • Does not change the original set
a = {1, 2, 3}
b = {2, 3, 4}

result = a.intersection(b)

print(a)
print(result)

Output:

{1, 2, 3}
{2, 3}

intersection_update() #

  • Changes the original set
  • Does not create a separate result set
a = {1, 2, 3}
b = {2, 3, 4}

a.intersection_update(b)

print(a)

Output:

{2, 3}

Choose intersection() when you want to keep the original data unchanged.

Common beginner mistakes #

Here are some common problems beginners run into with intersection():

  • Expecting a list instead of a set
  • Assuming the output order will match the input order
  • Forgetting that duplicates are removed in sets
  • Thinking the original set changes after calling intersection()

These are also common causes of confusion:

  • Using intersection() when order matters
  • Using sets with duplicate values and expecting duplicates in the result
  • Confusing intersection() with union() or difference()
  • Expecting the method to update the original set

Helpful debugging checks:

print(my_set)
print(type(result))
print(set1.intersection(set2))
print(len(result))

Example:

numbers = {1, 2, 2, 3}
other = {2, 3, 4}

result = numbers.intersection(other)

print(numbers)
print(result)
print(type(result))
print(len(result))

Output:

{1, 2, 3}
{2, 3}
<class 'set'>
2

Notice that the duplicate 2 does not appear twice, because sets automatically remove duplicates.

FAQ #

Does intersection() change the original set? #

No. It returns a new set and leaves the original set unchanged.

Can intersection() be used with more than two sets? #

Yes. You can pass multiple sets or iterables, and it keeps only items found in all of them.

Why is the result in a different order? #

Sets are unordered collections, so their display order is not guaranteed.

What happens if there are no common items? #

The method returns an empty set:

set()

See also #

Press Esc to close