Finding the Needle in a Haystack: Intersection of Sets in Python

It is always a hassle to pick the things that are common in sets of things. It is manageable if it is small but what will you do if there are huge stacks of data and you have been given the task to find the common elements from those data?

Thinking about that scenario itself makes my head spin but what if I tell you that there is a simple way in Python that you can employ to make this tedious task a simple one? Yes, you heard me right, there is a solution and you must've studied about sets in Python, here I am going to employ one of its operations which is Intersection.

In this article, you will learn in-depth about Intersection of sets in Python, its syntax, and finally, an example program to make a strong grasp of the concept. So, let's get started.

Intersection of Sets in Python:

You learned about sets in Python and in that, I briefly explained the concept of the superset in Python. We also discussed operations like union of sets, difference of sets, symmetric difference of sets, subset, superset and disjoint sets in Python in detail whereas this article focuses on the next operation of the set which is intersection.

You must've learned in-depth about the creation of sets in the article Set Comprehension. This article will be easy if you've already gone through it as this is the next step after creating sets.

Set intersections allow us to identify and extract the common elements present in two or more sets, enabling us to analyze shared characteristics or filter data efficiently. With Python's built-in functions and operators dedicated to set intersections, we gain a robust toolkit for solving a wide range of problems.

It's important to note that the intersection operation only returns the unique common elements between the sets. If there are duplicate elements within a set, the intersection will only include one copy of that element in the resulting set.

The intersection operation in Python can be performed using the 'intersection()' method or the '&' operator. Here's a breakdown of both approaches:

  • Using the 'intersection()' method: The 'intersection()' method is called on a set and takes one or more sets as arguments. It returns a new set that contains only the elements present in all the sets. Here's an example:
 >>> set1 = {1, 2, 3, 4}
 >>> set2 = {3, 4, 5, 6}
 >>> set3 = {4, 6, 8, 10}
 >>> intersection_set = set1.intersection(set2, set3)
 >>> print(intersection_set)
Output:

{4}

In this example, the 'intersection()' method is called on set1 and takes set2 and set3 as arguments. It returns a new set containing the common element '4' found in all three sets.

  • Using the '&' operator: The '&' operator can be used between sets to find their intersection. It returns a new set containing the common elements. Here's an example:
 >>> set1 = {1, 2, 3, 4}
 >>> set2 = {3, 4, 5, 6}
 >>> set3 = {4, 6, 8, 10}
 >>> intersection_set = set1 & set2 & set3
 >>> print(intersection_set)
Output:

{4}

In this example, the '&' operator is used between set1, set2, and set3. The resulting set will contain the common element '4'.

Conclusion:

Let us summarise whatever you learned up till now, the intersection of sets in Python is a powerful tool that can be used to get the common element among different sets.

In conclusion, understanding and leveraging the intersection of sets in Python opens up a world of possibilities for data analysis and manipulation. By utilizing the intersection() method or the & operator, we can effortlessly identify the shared elements among sets.

I hope this article has been helpful in explaining the concept of the Intersection of Sets in Python and I will see you in the next article with an amazing concept.


Learn via Video Course

Python Logo

Python

6109

7 hrs