Python Set intersection() Explained [Tutorial]

Introduction to Python Set intersection

In Python, a set is one of the most fundamental data structures for storing things sequentially. Curved brackets ‘{}’ denote the data elements of sets, and commas separate each element. You can delete and add new items to a set, but you can't change the ones already in the set because sets are unsorted, mutable, and unindexed. Like math, Python supports set intersection capabilities and generates a brand-new set as a result. In this post, we will examine Python set intersections, as well as a few methods for obtaining them, examples, and outcomes.

Python Set intersection() Explained [Tutorial]
The intersection of two sets

In Python, a set can be defined using the curly ‘{}’ brackets as shown below:

Advertisement
Set_A = {1,2,3,4}
Set_B = {4,5,6,7}

In the above code, we have defined two sets named Set_A and Set_B. We can see that both sets have only one common number: 4. Now if we want to intersect the above two sets then the resultant set will have only 4 because intersections take the common elements in the set. Let's discuss the intersection in detail.

 

How Python set intersection() works?

The process by which the shared elements or common elements of the two sets that were provided are combined to form a new set is referred to as the intersection of two sets. Since set intersection is not limited to two sets, you can find the shared items between any number of sets. Assume that set A and set B will intersect to create a set with all of their shared members.

Let's understand the set intersection better by looking at the following Venn diagram.

Let set A = {1,2,3,4,5,6,7}, and set B = {6,7,8,9,10,11}

Than A ∩ B = {6,7}

Python Set intersection() Explained [Tutorial]
Intersection of sets

There are only two elements that are common in both Set A and B, those elements are {6,7}. This is what intersections do, to get the common elements from two or more sets into one separate set.

Advertisement

 

Python Set Intersection Syntax

Python comes with a built-in function to manipulate sets which is the intersection() function. Today we will discuss how to perform set intersection using Python.

We have already seen how to define sets in Python using curly ‘{}’ braces. Let's understand the syntax of the intersection() function.

set A.intersection(set B, set C, set D, …)

In the above syntax, we are intersecting A with sets B, C, and D. The resultant set will have all the common elements from all these sets in a new separate set. We will see this by implementing some real examples.

 

Different examples to perform set intersection in python

We have already studied in detail about the set intersection. How intersection is performed mathematically and what intersections do. Now let's see some useful practical examples of set intersection using python. We will see the different ways to intersect sets.

 

Example-1: Using intersection() function

We are defining two sets named A, and B. we are intersecting set A with set B using the intersection() function. We are storing the resultant set in an A_intersect_B set.

A = {1,2,3,4,5,6,7}
B = {6,7,8,9,10,11}
A_intersect_B = A.intersection(B)
print(A_intersect_B)

The output of the above code snippet will be {6,7} because these two elements are the common elements in both sets A and B.

Advertisement

Output:

{6,7}

 

Example-2: Using the '&' operator

We can also achieve the above intersection using the intersection operator ‘&’ in python. To achieve this, we place the ‘&’ operator in between the two sets and the resultant set will have the common elements from both sets. Let's see this with an example:

A = {1,2,3,4,5,6,7}

B = {6,7,8,9,10,11}

We will place the ‘&’ operator for the intersection.

A ∩ B = A & B

A ∩ B = {6,7}

Now let's implement this using Python.

A = {1,2,3,4,5,6,7}
B = {6,7,8,9,10,11}
C = A & B
print(C)

In the above code snippet, we are not using the built-in <em>intersection()</em> function. Instead, we are using the intersection operator ‘&’ between A and B, we are storing the resultant set in C. the output of the above code snippet is given below. However, the intersection using the ‘&’ is easy but this also has some limitations which we will discuss ahead.

Output:

Advertisement
{6,7}

Now let’s see what result the intersection() and the ‘&’ give on two empty sets.

We know that the intersection of two empty sets is an empty set. For example:

A = {}

B = {}

A ∩ B = {}

Let's implement this in python using both ways.

A = {}
B = {}
C = set(A).intersection(set(B))
print('Intersection using intersection() : ',C)
D = set(A) & set(B)
print('Intersection using & operator : ',D)

In the above code snippet, we are intersecting an empty set A and empty set B using the intersection() first and then using the ‘&’ intersection operator. The resultant set from both will also be empty.

Output:

Intersection using intersection() :  set()
Intersection using & operator :  set()

The problem with the ‘&’ operator is that it cannot perform the intersection operations on lists. If we want to intersect a set with a Python list then ‘&’ will throw an error. Let’s see

Advertisement
A = {1,2,3,4}
B = [4,5,6,7]
C = A & B
print(C)

In the above code, we are intersecting a set called A with a list called B using the ‘&’ operator, this operation will through an error because the ‘&’ cannot support intersecting sets with lists.

Output:

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

The intersection() function will not through an error when you are intersecting a set with the list. Let’s see.

A = {1,2,3,4}
B = [4,5,6,7]
C = A.intersection(B)
print(C)

Output:

{4}

The resultant set has element 4 because this element is common in both sets A and list B.

We have seen the intersection of two sets until now. Let's have a look at intersecting three sets. Before going to jump into Python implementation, let's first understand how the intersection of the three sets works.

Advertisement

Let's take three set

A = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’,}

B = {‘c’, ‘d’, ‘e’, ‘f’, ‘g’,}

C = {‘e’, ‘f’, ‘g’, ‘i’, ‘j’,}

A ∩ B ∩ C = {e}

The resultant set from the above three sets will have {e} because e is the common element in all three sets. Let's implement this in Python.

Python Set intersection() Explained [Tutorial]
The intersection of three sets

Let’s say, there are three friends and they have been asked for their favorite fruits. Their answers are given below:

  • Jhon love to eat Apple, Mango, and Banana
  • Vicky’s favorite fruits are Graphs, Apple, and Guava
  • Lisa likes to eat Olives, Oranges, and apples.

Now from the above data, we can clearly see that ‘Apple’ is the common fruit between the three friends. Now let’s make sets of these three friends and implement the intersection between them in Python.

Jhon = {'Apple', 'Mango', 'Banana'}
Vicky = {'Graphs', 'Apple', 'Guava'}
Lisa = {'Olive', 'Oranges', 'Apple'}
Common_Fruit = Jhon.intersection(Vicky,Lisa)
print(Common_Fruit)

Output:

{'Apple'}

As we can see that the common fruit is Apple between the three friends. That’s what intersection does, to extract the common elements from the sets. Let's perform the above again using the ‘&’ operator.

Advertisement
Jhon = {'Apple', 'Mango', 'Banana'}
Vicky = {'Graphs', 'Apple', 'Guava'}
Lisa = {'Olive', 'Oranges', 'Apple'}
Common_Fruit = Jhon & Lisa & Vicky
print(Common_Fruit)

Output:

{'Apple'}

We got the same Apple as the common fruit. That’s all.

 

Summary

In this article, we studied how easily you can perform all the intersection operations. We saw that intersection is the extraction of all the common elements in the given sets. This article mentioned two intersection techniques using Python. The first intersection technique we saw is the <em>intersection()</em> function and the second way is to use the ‘&’ operator. The ‘&’ operator does not support the intersection of sets and lists together. We also saw the intersection of three or more sets together.

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X