What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python Set Intersection with Codes and Example

  • Dec 15, 2021
  • 5 Minutes Read
Python Set Intersection with Codes and Example

Python sets are one of the most fundamental data structures which enable you to store the elements in sequential form. The data elements in sets are represented inside the curly brackets, and commas separate each element. Sets are unordered, unchangeable, and unindexed, which means you can remove the set element and add new elements but cannot change the elements already present in the set. Like mathematics, python also enables the features of set intersection and generates the completely new set as the output. In this article, let us study what is python set intersections are and different methods to get the set intersection along with examples and output.

What is a Set Intersection?

The intersection of two sets can be defined as the formation of the new set containing the common elements of both the given sets. Set intersection is not a constraint between two sets, and therefore, you can have as many sets as you want to find the common elements between them. Suppose the intersection of set A and set B is a set containing all the elements present in set A and set B.

The below image shows the Venn diagram of set intersections, i.e., representing the common elements from both sets.

Example of set intersection

How to Find the Intersection of Sets in Python?

1) Using intersection() function

Python provides a large collection of inbuilt functions, one of which is the intersection() method. It is one of the common methods by which you can perform the python set intersection and is highly used among programmers. The syntax of the intersection function is as given below

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

 

Here, the intersection method takes a parameter as the set variable for which the common element is found. It returns as the new set containing the common elements of all the sets(set B, set C, set D…). Check out the below example to understand the working of the intersection() method.

For example:

X = {1, 3, 5, 7}
Y = {2, 3, 5, 8}

print(X.intersection(Y))

 

Output

{3, 5}

 

2) Using intersection operator(&)

Apart from the python inbuilt function, you can also perform set intersection using the '&' operator. Here, the operator is placed between both sets, and as a result, the completely new set is formed as the output containing the common elements of sets, as shown in the below example.

For example:

X = {1, 3, 5, 7, 8}
Y = {2, 3, 5, 8}
Z = {0, 2, 4, 8}

print(X & Y)
print(Y & Z)
print(X & Y & Z)

 

Output

{8, 3, 5}
{8, 2}
{8}

 

3) Using empty set for set intersection

Set intersection returns the common elements of any two sets, but what if the sets are empty and do not contain any elements in them. The output after performing intersection will also be an empty set as there are no common elements in the given sets.

For example:

X = {}
Y = {}
print(set(X).intersection(set(Y)))

 

Output

set()

 

Difference between set intersection() method vs set intersection operator (&)

To find the set intersection, the intersection() method accepts any iterable such as lists, strings, and dictionaries, etc. However, the set intersection operator only allows a set data structure to find the intersection.

As the intersection method accepts any iterable, note that it will convert every iterables to set before intersecting them and generate the output accordingly.

For example:

X = {1, 3, 5}
Y = [1, 5, 9]

result = X.intersection(Y)

print(result)

 

Output

{1, 5}

 

In the above example, the intersection is performed between a set and a list; however, the intersection method converted the list to set internally and generate the output. At the same time, the intersection operator will not accept any iterables and hence raise the error if you use them.

For example:

X = {1, 2, 3}
Y = [2, 3, 4]

result = X & Y
print(result)

 

Output

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

 

Summary

Python sets are the most fundamental data structure used to store sequential data into a single variable. Like in mathematics, python enables us to perform the set intersection and returns the common elements of both sets, which can be further used during programming. In this article, we studied various methods to find the set intersection with examples and output. It is most recommended to learn these methods and efficiently perform the explicit set intersection during programming in python.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.