Python set intersection() method returns a new set with an element that is common to all set
The intersection of two given sets is the largest set, which contains all the elements that are common to both sets. The intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B.
Python Set intersection() Method Syntax:
Syntax: set1.intersection(set2, set3, set4….)
Parameters:
- any number of sets can be passed
Return: Returns a set which has the intersection of all sets(set1, set2, set3…) with set1. It returns a copy of set1 only if no parameter is passed.
Python Set intersection() Method Example:
Python3
s1 = { 1 , 2 , 3 }
s2 = { 2 , 3 }
print (s1.intersection(s2))
|
Output:
{2, 3}
Example 1: Working of set intersection()
Python3
set1 = { 2 , 4 , 5 , 6 }
set2 = { 4 , 6 , 7 , 8 }
set3 = { 4 , 6 , 8 }
print ( "set1 intersection set2 : " ,
set1.intersection(set2))
print ( "set1 intersection set2 intersection set3 :" ,
set1.intersection(set2, set3))
|
Output:
set1 intersection set2 : {4, 6}
set1 intersection set2 intersection set3 : {4, 6}
Example 2: Python set intersection operator(&)
We can also get intersections using ‘&’ operator.
Python3
set1 = { 2 , 4 , 5 , 6 }
set2 = { 4 , 6 , 7 , 8 }
set3 = { 1 , 0 , 12 }
print (set1 & set2)
print (set1 & set3)
print (set1 & set2 & set3)
|
Output:
{4, 6}
set()
set()
Example 3: Python set intersection opposite
symmetric_difference() is an opposite to the Python Set intersection() method.
Python3
set1 = { 2 , 4 , 5 , 6 }
set2 = { 4 , 6 , 7 , 8 }
set3 = { 1 , 0 , 12 }
print (set1.symmetric_difference(set2))
print (set1.symmetric_difference(set3))
print (set2.symmetric_difference(set3))
|
Output:
{2, 5, 7, 8}
{0, 1, 2, 4, 5, 6, 12}
{0, 1, 4, 6, 7, 8, 12}
Example 4: Python set intersection empty
Intersection of empty sets returns an empty set.
Python3
set1 = {}
set2 = {}
print ( "set1 intersection set2 : " ,
set (set1).intersection( set (set2)))
|
Output:
set1 intersection set2 : set()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!