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
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
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
# 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
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
# 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
# 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.