Python Intersection
Table of Contents
In the last lesson, we learned about Python Union. Now letโs find the items that two or more sets have in common.
โ๏ธ Letโs Talk About What Two Sets Share
We have two sets. They each have some items. Some items are in both sets. Some are only in one.
The intersection of two sets is a new set with only the items that are in both sets. Thatโs it. The common items.
Think of it like two friends comparing their music playlists. The intersection is the songs they both have. The songs only one of them has are not included.
๐ The Intersection Operator & in Python
The simplest way to find the intersection of two sets is with the & operator.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}result = set1 & set2print(result)Output
{3, 4}Only 3 and 4 are in both sets. So the intersection is {3, 4}.
We can chain intersections.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}set3 = {4, 5, 6, 7}result = set1 & set2 & set3print(result)Output
{4}Only 4 is in all three sets. So the result is {4}.
๐ก The intersection() Method in Python
We can also use the .intersection() method. It does the same thing.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}result = set1.intersection(set2)print(result)Output
{3, 4}The method can take multiple arguments.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}set3 = {4, 5, 6, 7}result = set1.intersection(set2, set3)print(result)Output
{4}Same as chaining with &.
๐งช Intersection with Other Iterables in Python
Just like union(), the intersection() method can take any iterable. We can pass a list, a tuple, or a string.
set1 = {1, 2, 3, 4}list1 = [3, 4, 5]result = set1.intersection(list1)print(result)Output
{3, 4}The list is converted to a set internally, and then the intersection is found.
But the & operator only works with sets.
set1 = {1, 2, 3}list1 = [2, 3, 4]result = set1 & list1 # Error!Output
TypeError: unsupported operand type(s) for &: 'set' and 'list'So if we have a list, we use the .intersection() method.
๐ In-Place Intersection with intersection_update() in Python
The & operator and .intersection() method create a new set. The original sets are unchanged.
If we want to modify a set in place, we use the .intersection_update() method.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5, 6}set1.intersection_update(set2)print(set1)Output
{3, 4}Now set1 has been changed. It only contains the items that were in both sets.
We can also update with multiple sets.
set1 = {1, 2, 3, 4}set2 = {3, 4, 5}set3 = {4, 5, 6}set1.intersection_update(set2, set3)print(set1)Output
{4}And intersection_update() also accepts any iterable.
set1 = {1, 2, 3, 4}set1.intersection_update([3, 4, 5])print(set1)Output
{3, 4}๐ Python Intersection: A Real-Life Example
Letโs say we have two lists of skills. One from a job candidate. One from the job requirements. We want to find the skills they have in common.
candidate_skills = {"python", "java", "sql", "docker"}job_requirements = {"python", "sql", "aws", "docker"}
matching_skills = candidate_skills & job_requirementsprint(matching_skills)Output
{'python', 'sql', 'docker'}That tells us which required skills the candidate has. The ones missing are aws.
๐ Another Example โ Finding Common Friends
alex_friends = {"sam", "john", "emma", "david"}sam_friends = {"john", "emma", "chris", "david"}
common_friends = alex_friends.intersection(sam_friends)print(common_friends)Output
{'john', 'emma', 'david'}So Alex and Sam have three friends in common.
๐งญ Intersection with an Empty Set
Intersection with an empty set always gives an empty set. Because there are no common items.
set1 = {1, 2, 3}empty = set()result = set1 & emptyprint(result)Output
set()Thatโs expected.
โ ๏ธ Common Mistakes with Python Intersection
โ Mistake 1: Using & with a List
set1 = {1, 2}list1 = [2, 3]result = set1 & list1 # Error!So what do we do? We use the .intersection() method.
result = set1.intersection(list1)๐ซ Mistake 2: Thinking Intersection Modifies the Original
set1 = {1, 2, 3}set2 = {2, 3, 4}set1 & set2print(set1) # {1, 2, 3}The & operator returns a new set. It doesnโt change set1.
If we want to change set1, we use set1.intersection_update(set2) or set1 &= set2.
๐ Mistake 3: Confusing Intersection with Union
Union gives all items. Intersection gives only common items.
set1 = {1, 2}set2 = {2, 3}print(set1 | set2) # {1, 2, 3}print(set1 & set2) # {2}๐ Mistake 4: Forgetting That Intersection of Disjoint Sets Is Empty
If two sets have no common items, their intersection is an empty set.
set1 = {1, 2}set2 = {3, 4}print(set1 & set2) # set()Next, we will learn about Python Set Difference and find items that appear in one set but not another.