In this article, we are going to discuss Intersection in Python. After reading this article, you will master the concept of intersection in Python. We have also considered some illustrative examples to help you understand the subject better.

Let us start by understanding what an intersection is.

What is Python Set Intersection?

The intersection in Python refers to the common elements that are present in two or more sets, lists, or other iterable objects. When you take the intersection of two sets or lists, you get a new set or list that contains only the elements that are present in both of the original sets or lists.

In other words, it returns the values that are common between two or more objects. The intersection operation is often used in Python to perform various types of data analysis and processing tasks.

The intersection() method in Python can take one or more parameters depending on the type of iterable objects being used.

For sets The intersection() method can take one or more set objects as parameters.

For lists The intersection() method requires that the list objects be converted to sets using the set() constructor and can take one or more iterable objects as parameters.

Intersection Function in Python

Mathematically, given two non-empty sets, A and B, the intersection of A and B is defined as set C such that C contains only those elements common to A and B. It is represented by ∩. For example, if set A contains the elements {1, 2, 4, 8} and set B has {2, 6, 8, 10} as its elements, then A ∩ B is the set {2, 8}.

Taking the intersection of sets is a general approach to solving real-world problems.

We can do intersection in Python using two ways. Let us learn about both of these ways one by one.

Intersection Method in Python

One approach to performing set intersection in Python is using the intersection() method. It is an inbuilt function provided by the Python library. Let us take a look at its syntax.

Syntax

set1.intersection(set2, set3, set4…).

Here set1, set2, set3, etc., are sets of values. Set set1 is the base set that intersects with the other sets.

Parameter

This function can accept any number of sets as a parameter. This number can be zero as well.

In our example, set2, set3, and set4 are parameters of Insertion function in python.

Return Value

This method returns a set of values that are common to all the sets passed as a parameter. So it basically returns the intersection of all the sets: set1 ∩ set2 ∩ set3 ∩ set4…

Note that if we do not pass any parameter to this method, then it just returns the copy of the base set, i.e., set1. It simply does that because the intersection of a single entity is that entity itself.

Let us consider some examples.

Example 1

Implementation:

Python

Python

a = {1, 2, 3, 4, 5, 6, 7} b = {5, 6, 7, 8, 9, 10} z = a.intersection(b) print(z)

Output:

{5, 6, 7}

Explanation:

It is clear that sets a and b have only 5, 6, and 7 as common elements. Thus, the intersection function returns these numbers as a set.

Example 2

Implementation:

Python

Python

A = { “lokesh”, “neha”, “mehak” } B = { “lokesh”, “varun”, “shubh” } C = { “lokesh”, “shubh”, “priya” }

Z = A.intersection(B, C) print(Z)

Output:

{“lokesh”}

Explanation:

Even though sets B and C have “lokesh” and “shubh” as common elements, set A only has “lokesh” in common with the other two. Hence the integration function will output a set containing only one element, i.e. { “lokesh” }.

Another way to perform set intersection in Python is by using the intersection operator (&). It is an operator that allows us to intersect two or more sets of elements.

Syntax

setA = set1 & set2 & set3 & set4…

Here setA is the final set after the intersection of all the sets on the RHS.

Example 1

Implementation:

Python

Python

X = { 2, 4, 6, 9 } Y = { 6, 7, 8, 9 } Z = X & Y

print(Z)

Output:

{ 6, 9 }

Explanation:

As simple as it can be, the & operator intersects sets X and Y and returns the answer set as Z.

Note that if the sets are empty, their intersection is a null set.

From earlier examples, we can see that both the ways are quite similar to do set intersection in Python. However, it is not always true. There is one essential difference between the method and the operator.

The set intersection() method can accept any type of interables, such as lists, strings, and dictionaries. On the other hand, the intersection operator only allows sets as parameters.

In Python, intersection() is a built-in set method used to find the common elements between two or more sets. It returns a new set containing elements present in all input sets.

How do you show intersection in Python?

In Python, the intersection() method or the & operator is used to find the common elements between sets. It returns a new set containing elements shared by the input sets, demonstrating set intersection in Python.

How set intersection works in Python?

In Python, a set intersection is performed using the intersection() method or the & operator. It compares two or more sets and returns a new set containing only the elements present in all input sets, ensuring no duplicates.

What is intersection of two arrays Python?

In Python, the intersection operation on two arrays results in a third array that only contains the elements that exist in both arrays. You can find the intersection of two arrays by converting them into sets and using the intersection() method or the & operator.

Conclusion

In this article, we learned about the set intersection in Python. We discussed its two ways and considered some examples. We hope you enjoyed reading this article. If you wish to learn more about Python, you can refer to this amazing article by coding ninjas. We also suggest you check out our course on Python.

Visit our website to read more such blogs. Make sure you enroll in our other courses as well. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.