Python Set Intersection Operations

Introduction

Intersection of sets involves extracting common elements from sets, resulting in a new set that contains only those shared values. In Python, set intersection can be accomplished with the intersection() instance method, the intersection() class method, and the union operator, &, i.e. the ampersand character. Each of these options provides a slightly different capability and convenience. They work in a similar way to the options provided for the set union, and familiarity with those makes learning about intersection very easy.

Using the intersection() Instance Method

The intersection() instance method of a set object extracts the common elements between the set and one or more iterables, creating a new set that contains only those shared values.

Example:

set1 = {1, 2, 3}
set2 = {1, 2, 4}
lst1= [1, 2, 5]
intersection_set = set1.intersection(set2, lst1)
print(intersection_set)

Output:

{1, 2}

In this example, the intersection() method is used to find the common elements among the sets set1 and set2, and the list, lst1, resulting in a new set, intersection_set, containing the values 1 and 2.

Using the Python Set Intersection Operator &

The set intersection operator, &, can be used to find the common elements between two or more sets. Like the intersection() method, it creates a new set that contains only the shared values. However, it can only be used with proper set objects and not any other iterable type. Using & with iterable objects that are not sets results in an error.

Example:

set1 = {1, 2, 3}
set2 = {1, 2, 4}
set3 = {1, 2, 5}
intersection_set = set1 & set2 & set3
print(intersection_set)

Output:

{1, 2}

In this example, the intersection of three sets, set1, set2, and set3, is generated using the `&` operators between them.

Using the intersection() Class Method

You can also use the intersection() class method to find the intersection of a set and multiple iterable objects.

This method is a class method of the set class, and you call it with a set. prefix.

The difference between the intersection() instance method and intersection() class method is that the instance method is called on a specific set, whereas the class method includes the set as its first parameter. Following the first argument, which must be a set, the rest of the arguments can be objects of any iterable type.

Example:

set1 = {1, 2, 3}
set2 = {1, 2, 4}
lst1 = [1, 2, 5]
intersection_set = set.intersection(set1, set2, lst1)
print(intersection_set)

Output:

{1, 2}

The above example finds common elements of the sets, set1 and set2, and the list, lst1 using the intersection() class method.

Set Intersection with Mutable Sets

Similar to set union, set intersection operations do not modify the original set. Instead, they create a new set containing the intersection. If you want to modify the original set variable to contain the intersection result, you should do it explicitly by assigning the result back to it.

Example:

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.intersection(another_set) 
print(my_set)

Output:

{3}

After this operation, the original set, my_set, contains the resulting intersection, {3}.

Summary & Reference for Python Set Intersection

Set intersection is a fundamental set operation in Python for extracting common elements from two or more sets and other iterables into a new set.


The intersection() instance method extracts common elements among a set object and other iterable objects, creating a new set.

set1 = {1, 2, 3}
set2 = {1, 2, 4}
lst1= [1, 2, 5]
intersection_set = set1.intersection(set2, lst1)
print(intersection_set)  # --> {1, 2}

The set intersection operator, &, allows you to find common elements between two or more proper sets. It creates a new set containing only the shared values.

set1 = {1, 2, 3}
set2 = {1, 2, 4}
set3 = {1, 2, 5}
intersection_set = set1 & set2 & set3  # intersection_set --> {1, 2}

The intersection() class method of the set class lets you find common elements among a set object, given as the first argument, and other iterable objects, given as subsequent arguments.

set1 = {1, 2, 3}
set2 = {1, 2, 4}
lst1 = [1, 2, 5]
intersection_set = set.intersection(set1, set2, lst1)  # intersection_set --> {1, 2}

Set intersection operations don’t modify the original sets but create new sets. If you want to modify one of the original set variables with the intersection result, you should assign the result back explicitly.

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.intersection(another_set)  # my_set -->  {3}