Table of Contents

What Will You Learn
In this tutorial, you'll delve into Python's set intersection operations, understanding how to find common elements between sets using the intersection() method and the & operator. You'll learn to handle multiple sets, work with various iterable types, and apply these techniques in real-world scenarios. The guide also covers best practices and potential pitfalls, equipping you with the knowledge to efficiently manage set intersections in your Python projects.


When you're working with data, it's common to encounter overlapping values. Understanding which items appear in both datasets is crucial in many real-world scenarios — from filtering shared user IDs to identifying common tags or keywords. This is where the concept of set intersection comes in.

The intersection operation helps you extract only those elements that exist in all sets you're working with. Whether you’re cleaning data, comparing user input, or building a recommendation engine, this operation will save time and prevent logical errors. Learning how to use it properly gives you one more reliable tool in your programming toolbox.

How to Do Intersection of Sets in Python?

The most direct way to compute the intersection of two sets is by using either the & operator or the intersection() method. Both return a new set containing only elements that exist in both operands. These approaches are clean, readable, and efficient.

Here's the first example using the operator:


      set1 = {1, 2, 3, 4}
      set2 = {3, 4, 5, 6}
      result = set1 & set2
      print(result)  # Output: {3, 4}

The same result using the intersection() method:


      set1 = {'apple', 'banana', 'cherry'}
      set2 = {'banana', 'kiwi', 'apple'}
      result = set1.intersection(set2)
      print(result)  # Output: {'apple', 'banana'}

How to Find the Intersection of Two or More Sets in Python?

If you're comparing more than two sets, use the intersection() method and pass each set as an argument. Python handles all sets at once and returns only those elements present in every one.


      a = {1, 2, 3, 4}
      b = {2, 3, 5}
      c = {3, 2, 9}
      result = a.intersection(b, c)
      print(result)  # Output: {2, 3}

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

Common Mistakes Made by Beginners

Using Lists Instead of Sets

One of the most common errors is trying to use intersection() on lists. Lists in Python don’t support set operations directly, which leads to an AttributeError.


      # Wrong
      list1 = [1, 2]
      list2 = [2, 3]
      print(list1.intersection(list2))  # AttributeError

To fix this, convert your lists into sets before performing the intersection.


      # Correct
      result = set(list1).intersection(set(list2))
      print(result)  # Output: {2}

Forgetting the Return Value

The intersection() method returns a new set and does not modify the original. Beginners often expect it to alter the calling set in place.


      # Wrong
      a = {1, 2, 3}
      b = {2, 3}
      a.intersection(b)
      print(a)  # Still {1, 2, 3}

If you want to update the original set, use intersection_update() instead.


      # Correct
      a.intersection_update(b)
      print(a)  # Output: {2, 3}

Not Handling Empty Results

If sets have no common elements, the result is an empty set. Beginners may forget to check for this case, which might break further logic.


      a = {1, 2}
      b = {3, 4}
      result = a & b
      print(result)  # Output: set()

Always check if the result is empty using if not result: to avoid logical errors.

Mixing Data Types in Sets

Mixing incompatible data types like strings and integers in set intersection may lead to unexpected results — the intersection will simply be empty.


      a = {1, 2, '3'}
      b = {'3', 4}
      print(a & b)  # Output: {'3'}

Ensure your sets have matching data types if you want meaningful intersections.

Using Intersection on Non-Hashable Elements

Sets in Python require elements to be hashable. Attempting to add lists or dictionaries to a set will raise a TypeError, and intersection will fail.


      # Wrong
      a = {[1, 2], [3, 4]}  # TypeError

Always use immutable and hashable elements like integers, strings, or tuples inside sets.

FAQ

Which operator would you use to find the intersection of 2 sets in Python?

The intersection operator in Python is the & symbol. It is a shorthand for calling the intersection() method and is widely used for its readability and simplicity. This operator returns a new set that includes only elements present in both sets.

For example, if you write {1, 2, 3} & {2, 3, 4}, the result will be {2, 3}. This operator works only with set objects. If you try to use it with lists or dictionaries, it will raise a TypeError. Always ensure both operands are sets before using the & operator.

How to get intersection of two sets in Python?

To get the intersection of two sets, use either the intersection() method or the & operator. Both approaches return a new set containing elements that appear in both original sets. These methods are essential when working with overlapping data in collections.

Here's a simple example: {'a', 'b', 'c'}.intersection({'b', 'c', 'd'}) returns {'b', 'c'}. You can also write it as {'a', 'b', 'c'} & {'b', 'c', 'd'}. Both are valid and produce the same result. Remember, these methods do not change the original sets — they return a new one.

Can I intersect more than two sets in Python?

Yes, you can intersect multiple sets by passing them as arguments to the intersection() method. This is useful when you need to compare three or more datasets and find common elements across all of them. The syntax is clean and concise: set1.intersection(set2, set3, set4).

Alternatively, you can use set.intersection(set1, set2, set3) as a class method. Both approaches ensure you get a result containing only elements present in every set provided.

What if two sets have no elements in common?

If there are no common elements between two sets, the intersection result will be an empty set: set(). This is not an error but an expected outcome. Many beginners mistakenly think this is a bug or issue with their logic.

Always account for this case in your program by checking if the result is empty using if not result:. This helps avoid unexpected behavior in follow-up logic like loops or conditions based on the intersection's content.

What is the difference between intersection and intersection_update?

The intersection() method returns a new set and leaves the original sets unchanged. In contrast, intersection_update() modifies the calling set directly by keeping only the elements found in both sets.

Use intersection() when you want to keep your original sets intact and need a fresh result. Use intersection_update() when you're working with large sets and want to save memory or update data in place.