Table of contents
1.
Introduction
2.
What is Intersection() function in Python?
3.
Syntax of Set intersection() in Python
4.
Parameter of Set intersection() in Python
5.
Python Set intersection() Method Example:
5.1.
Example 1
6.
Python
6.1.
 
6.2.
Example 2
7.
Python
7.1.
Python
8.
Intersection Operator(&) in Python
8.1.
Example 1 of Intersection Operator(&) in Python
8.2.
Python
9.
Intersection() Method vs. Intersection Operator
9.1.
Example 1
9.2.
Python
9.3.
Example 2
9.4.
Python
10.
Frequently Asked Questions
10.1.
What is the opposite of intersection in Python?
10.2.
How do you show intersection in Python?
10.3.
How set intersection works in Python?
10.4.
What is intersection of two arrays Python?
11.
Conclusion
Last Updated: Oct 7, 2024
Easy

Python Set intersection() Method

Author Lokesh Sharma
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The intersection() method in Python is used to find the common elements between two or more sets. It returns a new set containing only the elements that are present in all the sets on which the intersection() method is called. This method is useful when you need to identify the overlapping elements between different sets, such as finding the shared items or common characteristics among multiple groups.

Intersection in Python

In this article, we will learn about the intersection method in detail with syntax and proper examples to understand this in better way.

What is Intersection() function in Python?

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 Set 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 of Set intersection() in Python

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.

Python Set intersection() Method Example:

Let us consider some examples.

Example 1

  • Python

Python

a = {1, 2, 3, 4, 5, 6, 7} 
b = {5, 6, 7, 8, 9, 10}
z = a.intersection(b)
print(z)
You can also try this code with Online Python Compiler
Run Code

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

  • Python

Python

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

Z = A.intersection(B, C)
print(Z)
You can also try this code with Online Python Compiler
Run Code

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

Example 3

  • Python

Python

# Define two sets
set_a = {'red', 'blue', 'green', 'yellow'}
set_b = {'green', 'yellow', 'orange', 'purple'}

# Find the intersection
common_colors = set_a.intersection(set_b)

# Print the result
print(common_colors)
You can also try this code with Online Python Compiler
Run Code

Output : 

{'green', 'yellow'}

Explanation : 

In this example, the intersection of set_a and set_b results in {'green', 'yellow'}, which are the colors common to both sets.

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 of Intersection Operator(&) in Python

 

  • Python

Python

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

print(Z)
You can also try this code with Online Python Compiler
Run Code

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. 

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:

Example 1

 

  • Python

Python

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

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

commonMarks = marksA.intersection(marksB)
print(commanMarks)
You can also try this code with Online Python Compiler
Run Code

 

Output:

{ 19, 45 }

Example 2

 

  • Python

Python

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

commonMarks = marksA & marksB
print(commonMarks)
You can also try this code with Online Python Compiler
Run Code

 

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 the opposite of intersection in Python?

The opposite of intersection in Python is the symmetric difference of sets. You can obtain it using the symmetric_difference() method or the ^ operator. It returns elements that are in either set but not in both.

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. We also suggest you check out our course on Python.

Check out this article - 

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. 

Live masterclass