Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Python Set Intersection?
2.1.
Syntax of intersection() in Python
2.2.
Parameter Values
3.
Intersection Function in Python
4.
Intersection Method in Python
4.1.
Parameter
4.2.
Return Value
4.2.1.
Example 1
4.3.
Python
4.3.1.
Example 2
4.4.
Python
5.
Intersection Operator in Python
5.1.
Example 1
5.2.
Python
6.
Intersection() Method vs. Intersection Operator
6.1.
Case1
6.2.
Python
6.3.
Case2
6.4.
Python
7.
Frequently Asked Questions
7.1.
What is intersection () in Python?
7.2.
How do you show intersection in Python?
7.3.
How set intersection works in Python?
7.4.
What is intersection of two arrays Python?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python Set intersection() Method

Author Lokesh Sharma
2 upvotes
gp-icon
Basics of python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

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.

Intersection in Python

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.

To know about the Fibonacci Series in Python here. 

Syntax of intersection() in Python

The syntax for the intersection() method is:

For sets:

set1.intersection(set2, set3, ..., setN)


For lists:

set(list1).intersection(list2, list3, ..., listN)

Parameter Values

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Function in Python
Intersection in python
Intersection Function in Python

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” }.

Also see, Python Filter Function

Intersection Operator in Python

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. 

Check out Escape Sequence in Python

Intersection() Method vs. Intersection Operator

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. 

Let us consider one example:

Case1

Code:

  • Python

Python

# set
marksA = { 20, 19, 34, 45 }

# list
marksB = { 30, 19, 22, 45 }

commonMarks = marksA.intersection(marksB)
print(commanMarks)

 

Output:

{ 19, 45 }

Case2

Code:

  • Python

Python

# set
marksA = { 20, 19, 34, 45 }
# list
marksB = { 30, 19, 22, 45 }

commonMarks = marksA & marksB
print(commonMarks)

 

Output:

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

 

It is clear from the above examples that the operator does not support intersection between different iterables.

You can practice by yourself with the help of online python compiler.

Frequently Asked Questions

What is intersection () in Python?

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.

Check out this article - Quicksort 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.

Previous article
Python Pow() Function
Next article
Python String Swapcase() Method
Guided path
Free
gridgp-icon
Basics of python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass