In this Python tutorial, we’ll learn about the role of Python set intersection method. We’ll walk ourselves through multiple Python code examples in order to practically understand the usage of intersection method in Python set.
Introduction: Python Set Intersection Method
This method is used to return a new Python set which contain items that are common in all specified Python sets.
Let’s first walk ourselves through its syntax. After that, we’ll jump right into its practical implementation.
Syntax of intersection() Method
setA.intersection(sets or just one set)
- This method can contain arbitrary number of arguments(sets in this case). We can pass single or multiple sets to it.
- The intersection() method returns a new set that contain items which are present/common in all sets. If nothing is passed as an argument, then it returns a shallow copy of the set (setA in this case).
Understanding Python Set Intersection Method (Multiple Examples)
See below examples in order to understand the working of intersection() method in Python set.
Example 1: intersection() on Two Sets
a={1,8,9,7} b={5,8,4,1,6} print( a.intersection(b) )
Output
{8, 1}
- In the above program, we’ve created two sets.
- After that, we’ve used intersection method of first set(a) and have passed second set(b) as an argument.
- Finally, we’ve used it inside print function to display the returning set. We can see that the new set consist of items that are common in both sets.
Example 2: intersection() on Multiple Sets
a={99,77,88,55} b={22,55,77,11} c={55,4,2,22,77} print( a.intersection(b,c) )
Output
{77, 55}
In this program, we’ve created three Python sets. We can provide multiple sets as an argument to the intersection method. The resulting set will consist of items that are common in all sets.
Example 3: When no Common Items are Found
a={'a','b','c'} b={'d','e','f'} print( a.intersection(b) )
Output
set()
In the above program, we’ve specified 2 sets. After that, we’ve applied intersection method on them. We can see that the intersection method returns set() which denotes an empty Python set. It means no common items were found.
Example 4: Python Set Intersection Method Return Value
a={'aa','bb','cc',44,7,8} b={'aa','h','k',44,2,9} returnVal=a.intersection(b) print('The returned value is: ',returnVal)
Output
The returned value is: {44, 'aa'}
- First, we’ve created two sets.
- Secondly, we’ve applied intersection method on them. Also, we’ve stored the returned value inside a variable.
- Finally, we’ve displayed value of this variable using print function.
- As a result, it shows a new set which contain items that are common in both sets.
It means Python set intersection method returns a set which consist of items that are common in all sets.
Example 5: No Argument Passed To intersection() Method
setA = {4.5,6,3.9} retVal = setA.intersection() print(retVal)
Output
{3.9, 4.5, 6}
We’ve passed nothing as an argument to the intersection method. We also have stored the return value inside a variable. Then we’ve displayed the value of this variable using Python print function. It shows a shallow copy of the its set (set A in this case).
Example 6: Python For Loop to find intersection() between List of Sets
setLsA=[{4.5,1},{5,6},{3,5,5},{'v','r'}] setLsB=[{10,1},{12,64,5,6},{8,9,7},{234,'v','r','j',}] newLst=[] for v1,v2 in zip(setLsA,setLsB): newLst.append(v1.intersection(v2)) print(newLst)
Output
[{1}, {5, 6}, set(), {'v', 'r'}]
- In this program, we’ve specified two Python list of sets.
- An empty list has also been specified which will store the result(sets).
- Then we’ve used for loop which will take each item from list(these items are sets) while iterating over it. Click here to learn more about iterating over multiple lists simultaneously.
- In each iteration, the intersection method will return a set which will be stored in the new list by using the append method of Python list.
- Finally, we’ve displayed the new list to see the result. The result can be seen in the above output block.
Example 7: Using & operator To Find Common Items Between Sets
a = {100,200,300,400} b = {200,300,400,500,600} c = {300,500,400,600,900} res1 = a & b res2 = a & b & c print(res1) print(res2)
Output
{200, 300, 400} {400, 300}
We’ve created three Python sets and have applied & operator on all of them. So its clear that we can easily find common items between sets using & operator as well. The result can be seen in the above output section.
So this is the proper usage of Python set intersection method. We hope you’ve learned something new from this tutorial. If so, then feel free to share it.
Do ask if you still have any queries regarding Python set intersection method. We’ll be super glad to solve all your queries.
Conclusion
In conclusion, hope you now have a proper practical knowledge of how Python set intersection method works. Do give it a try and don’t forget to share your worth reading feedback with us.
You can discover more concepts about Python programming in this site. Thank you for reading this article.