We can compare different sets and find the common items in these sets with Python Set Intersection method. This can be done with this set method or we can use “&” operator to do this. To understand how to use Python Set Intersection, let’s show some examples.
Below, we will compare two python set and find the common items in it. The return of this code will be also a set that includes these common items.
You can also learn Python Set Operations
The below code with “&” operator will also do the same thing with Python Set Intersection method.
You can also checck Python Set Union Method
You can also watch the video of this lesson!
Now, let’s compare more than two sets with the help of Python Set Intersection. Below, we will compare three sets and find the common items used in these python sets.
The output of this python code will be 10 in a set, because in all sets above, 10 is common.
The same code with “&” operator will give the same output:
Let’s give more examples for intersection with string sets. Below, we will compare two python sets. These are two string sets.
We can use “&” operator to get the same result like below:
In this lesson, we have talked about Python Set Intersection method. We have compared different sets and find the common items in these sets with both this method or “&” operator. These methods are used especially to find the common members of different items. You can increase your coding examples for this lesson by yourself in this Python Programming Course.