Intersection of Sets – Python

We know about various properties of Sets. Some of the key properties of Sets are:

  • Set is an unordered, mutable, iterable collection datatype that can store heterogeneous elements.
  • Set does not allow to store duplicate elements in it.
  • Set in Python supports mathematical operations like union, intersection, difference, and symmetric difference, subset and super-set.

In this article, we will learn about Intersection operation on Sets in Python.

Intersection of Sets

Intersection of Sets is a fundamental concept in mathematics Set theory and in programming. Intersection of Sets refers to the operation of selecting common elements between two or more Sets. Intersection of Set will return a new resulting set. The result-set of the intersection operation contains only those elements which are present in all of the original sets given for intersection. The result-set of intersection operation will not any duplicate elements.

Keys points from Intersection of Set

  1. Intersection of Set is an operation that select elements which are present in each given set, into a single set (result-set).
  2. Sets are unordered collections, so the order of elements in the result set is not defined.
  3. Sets does not allow duplicate elements. Intersection result set does not contain any duplicate elements.

Intersection of Sets – Mathematical Definition

For two sets A and B, the intersection of A and B, denoted A ∩ B and is defined as:

A B = { x x A and x B }

Set A

Set A

Set B

Set B

A B

Set A Intersection Set B
A B

In Python, we have multiple ways to perform Intersection operation on the Sets.

  • Using intersection_update() method – intersection_update() method is called on a Set and other Sets are passed as an argument. intersection_update() method does not return any value, but it updates the original Set with intersection elements.
  • Using intersection() method – intersection() method returns the result-set with intersection of Set elements.
  • Using and (&) operator – and (&) operator returns the result-set with intersection of Set elements.

Intersection of Sets – using intersection_update() method

  • intersection_update() method does not return any result-set
  • set_1.intersection_update(set_2) operation will update the set_1 with intersecting elements.
# using intersection_update() method
print("intersection_update() method")

set_1 = {'Python','AWS','Java','Azure','ML'}
set_2 = {'Python', 'AWS', 'NumPy', 'Pandas', 'Matplotlib'}

set_intersection = set_1.intersection_update(set_2)        # intersection_update() method

print(set_1)            # intersection elements should be updated in set_1
print(set_2)
print(set_intersection)

In this program, we have defined two sets referenced by variables ‘set_1’ and ‘set_2’. We are using built-in intersection_update() method on first set and second set is passed as an argument to intersection_update() method. intersection_update() method will not return the result-set with intersection elements. Intersection elements are updated in the original set ‘set_1’ (Set on which intersection_update() method was called). Returned value assigned to a variable ‘set_intersection’ should be None. At end we are printing all the sets.

Program Output

intersection_update() method
{'Python', 'AWS'}           # intersection elements
{'Pandas', 'AWS', 'Matplotlib', 'Python', 'NumPy'}           # original set set_2
None                            # None returned from intersection_update() method

From the program output, ‘set_1’ elements are updated with the result of intersection operation. Now ‘set_1’ contains only two elements (common elements) which were present in both the sets. intersection_update() method did not return any value and its value is None.

Intersection of Sets – using intersection() method

  • intersection() method will return the result-set with elements which are present in all intersecting sets.
  • Syntax – set_1.intersection(set_2)
# using intersection() method
print("intersection() method")

set_1 = {'Python','AWS','Java','Azure','ML'}
set_2 = {'Python', 'AWS', 'NumPy', 'Pandas', 'Matplotlib'}

course_intersection = set_1.intersection(set_2)       # intersection() method

print(set_1)
print(set_2)
print(course_intersection)           # intersection result-set

In this program, we have defined two sets referenced by variables ‘set_1’ and ‘set_2’. We are using built-in intersection() method on first set and second set is passed as an argument to intersection() method. intersection() method will return the intersection result-set. We are getting the returned Set in variable ‘course_intersection’. At end we are printing the elements of all sets.

Program Output

intersection() method
{'Python', 'Azure', 'ML', 'Java', 'AWS'}                         # original set set_1
{'Python', 'Matplotlib', 'NumPy', 'Pandas', 'AWS'}    # original set set_2
{'Python', 'AWS'}                            # intersection result-set

From the program output, there is no change in the elements of ‘set_1’ and ‘set_2’. After the Intersection operation, we got a result set ‘course_intersection’. Result set contains elements which were present in both the sets. It removes all elements which were not present both intersecting sets.

Using intersection() method with 3 sets

intersection() method can take single or multiple Sets on which we want to perform Intersection operation. There are two ways we can do Intersection of more than 2 Sets.

  1. set_1.intersection(set_2).intersection(set_3) – In this case we are doing intersection operation two times. First with sets set_1 and set_2 and then with their result_set and set_3.
  2. set_1.intersection(set_2, set_3) – In this case we are passing multiple sets to the intersection() method. So, 1 time intersection operation can be done with multiple sets.
# using intersection() method with three sets
print("using intersection() method with three sets")

set_1 = {10, 20, 30, 40}
set_2 = {20, 30, 40, 50, 60}
set_3 = {40, 50, 60, 70, 80, 90}

number_intersection_1 = set_1.intersection(set_2).intersection(set_3)             # using intersection() 2 times
print("set_1 intersection set_2 intersection set_3 - ", number_intersection_1)

number_intersection_2 = set_1.intersection(set_2, set_3)             # using intersection 1 time, passing multiple sets
print("set_1 intersection set_2 intersection set_3 - ", number_intersection_2)

In this program, we have defined three sets referenced by variables ‘set_1’, ‘set_2’ and ‘set_3’.

In the first case – we are using built-in intersection() method on ‘set_1’ and passing ‘set_2’ as argument to intersection() method. We are using intersection() method to this result-set and passing the third set ‘set_3’. Final result-set is referenced by variable ‘number_intersection_1’.

In the second case – we are using intersection() method on ‘set_1’ and passing both sets as an argument. Final result-set is referenced by variable ‘number_intersection_2’.

Program Output

using intersection() method with three sets
set_1 intersection set_2 intersection set_3 -  {40}              # output result-set 1
set_1 intersection set_2 intersection set_3 -  {40}              # output result-set 2

From the program output, after the Intersection operation in both the cases we got result-set. Result set contains elements which are present all three sets. Only 40 is present in all three sets. In both cases, result-set has similar elements.

Intersection of Sets – using and (&) operator

  • Syntax – set_1 & set_2
# intersection using & operator
print("join of sets - intersection using & operator")

set_1 = {10, 20, 30, 40}
set_2 = {20, 30, 40, 50, 60}
set_3 = {30, 40, 50, 60, 70, 80, 90}

number_intersection_1 = set_1 & set_2                     # and (&) on two sets
print("number_set_1 U number_set_2 - ", number_intersection_1)

number_intersection_2 = set_1 & set_2 & set_3             # and (&) on three sets
print("set_1 intersection set_2 intersection set_3 - ", number_intersection_2)

We have defined three sets referenced by variables ‘set_1’, ‘set_2’ and ‘set_3’.

In the first case – we are using and (&) operator with ‘set_1’ and ‘set_2’. So, it an intersection of two sets.

In the second case – we are using and (&) operator with ‘set_1’, ‘set_2’ and ‘set_3’. So, it an intersection of three sets.

Program Output

join of sets - intersection using & operator
number_set_1 U number_set_2 -  {40, 20, 30}                 # intersection of 2 sets
set_1 intersection set_2 intersection set_3 -  {40, 30}    # intersection of 3 sets

After the Intersection operation, we got a result set in both cases. Result set contains elements which are present in all intersecting sets.

Summary

In this article we learn about various Intersection operation on Sets. Following scenarios were explored:

Code – Github Repository

All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.

Python Topics


Interview Questions & Answers

Q: What happens if we perform an intersection on two sets with no common elements?

Intersection of two sets with no common elements, will be an empty set. Example:

set_1 = {10, 20, 30, 40}
set_2 = {54, 66, 78}

intersection_set = set_1 & set_2
print(intersection_set)  # Empty intersection_set => set()

Q: What is the time complexity for intersection of two sets?

In Python, time complexity for intersection of two sets is O(min(len(set_1), len(set_2))). This is because Python will iterate over the smaller set and check if each element exists in the larger set using a hash lookup, which is O(1).

If we will have N number of set. Then time complexity for intersection of N number of sets will be O(min(len(set_1), len(set_2), …… , len(set_N))).

Q: How can we use set intersection in real-world problems?

There are multiple real-world scenarios or applications, where set intersection can be useful.

  • Filtering common items – To get the list of customers who have purchased products every month in last current year. This information is useful to get customer insight and helps in business analytics.
  • Finding common elements in datasets – While analyzing large datasets, finding common elements between different sets can be crucial, like common words in documents, common users in different user groups, etc.

Q: Can the intersection of sets is possible with mixed data types elements?

Yes, intersection if sets is possible with sets having mixed datatypes elements. But the elements in that set should be hashable (e.g., integers, strings, tuples). The intersection of sets will work as expected, finding common elements based on their values. Example:

set_1 = {32, "shbytes", (45, 78)}
set_2 = {"shbytes", 42, (45, 78)}

intersection_set = set_1 & set_2
print(intersection_set)  # Elements of intersection_set => {(45, 78), 'shbytes'}

Q: What will happen if we intersect a set with a non-set type, like a list or a tuple?

Intersect a set with a non-set type, directly (without converting to set) using the & operator, Python will raise a TypeError. We can convert the list or tuple to a set first and then perform the intersection.

set_1 = {10, 20, 30, 40}
list_1 = [20, 30, 40]

#intersection_set_list = set_1 & list_1 
# Direct intersection would raise TypeError:
# TypeError: unsupported operand type(s) for &: 'set' and 'list'

# Convert list to set
intersection_set = set_1 & set(list_1)
print(intersection_set)  # Elements in intersection_set => {40, 20, 30}

Q: What are the things to be cautious about when working with set intersections?

  1. Order not preserved – Sets are unordered, result for intersection of sets will not preserve any order from the original sets.
  2. Mutable types cannot be set elements – Set does not support unhashable elements like lists or dictionaries. Python will raise a TypeError, if we intersect sets with unhashable elements like lists or dictionaries.
  3. Empty intersections – Intersection of sets can be empty. Ensure to handle cases where the intersection might be empty, which could impact subsequent operations.
  4. Performance considerations – While set operations are generally fast, working with extremely large sets could still be expensive and require high computation. So optimizing the size of the sets before intersection can be beneficial.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *