Python Set Intersection - The Ultimate Guide for Beginners

Python Set Intersection - The Ultimate Guide for Beginners

In the last week’s article, you’ve learned what Python set difference() is all about. Now we’ll explore another commonly used set function. Python set intersection allows you to find common elements between two or more sets. It has many useful applications, such as finding common skills between job applicants.

Today you’ll learn everything there is about Python set intersection. You’ll be ready to use it with confidence after reading the article.

The best part? You don’t have to read at all, as I’ve covered the topic in a video format:


Python Set Intersection - The Basics

So, what is Python set intersection? That’s what we’ll answer in this section. You’ll get a complete understanding of the definition, syntax, and return values through visual examples.

Definition and usage

Set intersection function returns the common element(s) of two or more sets. For example, I like to code in Python, JavaScript, and PHP, and you use Java, Python, and Ruby. We both have Python in common. We’ll work with two sets throughout the article just to keep things simple, but the same logic applies when intersecting more of them.

Take a look at the following two sets:

Image 1 - Two sets with programming languages (image by author)

Image 1 - Two sets with programming languages (image by author)

Calculating an intersection between these sets means we’ll get a new set with a single element - Python. Why? Because it’s the only element common to both sets:

Image 2 - Set intersection in action (image by author)

Image 2 - Set intersection in action (image by author)

The set intersection in Python is commonly represented with a Venn diagram. Here’s what it looks like:

Image 3 - Set intersection as a Venn diagram (image by author)

Image 3 - Set intersection as a Venn diagram (image by author)

Only Python is common to both sets, so I’ve highlighted the intersection area in blue.

What does intersection method do in Python and how do you find the intersection between sets? Let’s go over the syntax to answer these questions.

Syntax

# Intersection of two sets
set1.intersection(set2)

# Intersection between multiple sets
set1.intersection(set2, set3)

Where:

  • set1 - The iterable to find intersection from.
  • set2, set3 - Other sets use to calculate intersection.

Return value

The intersection function returns a new set which is the intersection between the first set and all other sets passed as arguments - but only if set(s) or iterable object(s) were passed to the function.

If no arguments were passed into the intersection() function, a copy of the set is returned.


Python Set Intersection Function Example

We’ll declare two sets, just as on Image 1:

  • A: Contains Python, JavaScript, and PHP
  • B: Contains Java, Python, and Ruby

Only Python is present in both sets, so calculating an intersection should return a new set with Python being the only element:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}

print(f"A ∩ B = {A.intersection(B)}")

Output:

A ∩ B = {'Python'}

If you don’t specify any parameters to the intersection function, a copy of the set is returned:

print(f"A ∩ / = {A.intersection()}")

Output:

A ∩ / = {'JavaScript', 'PHP', 'Python'}

You can verify it was copied by printing the memory address:

A = {'Python', 'JavaScript', 'PHP'}
A_copy = A.intersection()

print(hex(id(A)))
print(hex(id(A_copy)))

Output:

0x108c68e40
0x108da5ac0

You won’t see the identical values, and that’s not the point. The important thing is that they’re different, indicating the set was copied to a different memory address.

One commonly asked question is how to find intersection of two lists, so let’s answer it now.

How to find intersection of two lists in Python?

List is the most popular data type in Python - it’s used even in cases where a better data structure exists. There are numerous ways you find the intersection of two lists. We’ll cover two:

  1. list_intersection() - By using list comprehensions.
  2. list_intersection2() - By converting a single list to a set.
def list_intersection(l1: list, l2: list) -> list:
    return [val for val in l1 if val in l2]

def list_intersection2(l1: list, l2: list) -> list:
    return list(set(l1).intersection(l2))


A = ['Python', 'JavaScript', 'PHP']
B = ['Java', 'Python', 'Ruby']

print(f"A ∩ B v1 = {list_intersection(A, B)}")
print(f"A ∩ B v2 = {list_intersection2(A, B)}")

Output:

A ∩ B v1 = ['Python']
A ∩ B v2 = ['Python']

Python Set Intersection Using the & Operator

You don’t have to call the intersection() function every time. You can use the & operator instead:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}

print(f"A ∩ B = {A & B}")

Output:

A ∩ B = {'Python'}

Everything else remains the same. Remember that both operands must be of type set for & operator to work.


Python Set Intersection Common Errors

You’re likely to encounter errors when you first start working with sets. These are common, but usually easy to debug.

AttributeError: ‘list’ has no attribute intersection

This is the most common error and it happens when you call the intersection() function on a wrong data type. Only sets have this function built in.

Here’s an example - an exception is raised when you use lists:

A = ['Python', 'JavaScript', 'PHP']
B = ['Java', 'Python', 'Ruby']

print(f"A ∩ B = {A.intersection(B)}")

Output:

Image 4 - Attribute error on the set intersection (image by author)

Image 4 - Attribute error on the set intersection (image by author)

Make sure both are of type set and you’ll be good to go.

TypeError: unsupported operand type(s) for &: ‘set’ and ’list’

This error occurs when you try to use shorthand notation (& sign) on invalid data types. Both operands must be of type set for the shorthand notation to work. Here’s an example:

A = {'Python', 'JavaScript', 'PHP'}
B = ['Java', 'Python', 'Ruby']

print(f"A ∩ B = {A & B}")

Output:

Image 5 - Type error on the set intersection (image by author)

Image 5 - Type error on the set intersection (image by author)

As you can see, A is a set and B is a list, so the & sign doesn’t work.


Python Set Intersection FAQ

We’ll now go over a couple of frequently asked questions (FAQ) regarding the Python set intersection function.

Set intersection vs. Intersection update - What’s the difference?

The intersection_update() function removes items that are not found in both sets (or multiple sets). The function is different from the intersection() function, as it removes unwanted elements from the original set and doesn’t return anything:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}

A.intersection_update(B)
print(A)

Output:

{'Python'}

What’s the opposite of Python set intersection?

The opposite of Python set intersection is the symmetric difference - all elements that appear only in the first or the second set, but not in both:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}

print(A.symmetric_difference(B))

Output:

{'Ruby', 'Java', 'PHP', 'JavaScript'}

You can calculate the symmetric difference using the ^ operator:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}

print(A ^ B)

Output:

{'Ruby', 'Java', 'PHP', 'JavaScript'}

What’s the time complexity of set intersection in Python?

The time complexity of set intersection in Python on a set with n elements and a set argument with m elements is O(min(n, m)) because you need to check for the smaller set whether each of its elements is a member of the larger set. Checking membership is O(1), according to finxter.com.


Conclusion

Python set intersection is utterly simple to understand. We went through the intuition and definition with visual examples and built our way towards understanding more advanced usage and typical errors you’ll encounter. We also answered some frequently asked questions.

I hope that this article has helped you develop a better understanding of the Python set intersection function. As always, if you have any questions or comments, please feel free to ask in the comment section below. Happy coding!

Learn More

Stay connected