Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Set Intersection

Python Set Intersection basically finds and returns elements common among the sets.

Syntax:

set1.intersection(set2, set3, ... setN)

set1.intersection(set2) is equivalent to set1 ∩ set2.

Arguments: Accepts one or more sets as an argument.

Return Value: Returns a set as output which contains elements common to all the sets.


Ways to achieve Set Intersection in Python

Either of the following ways can be used to perform Python Set Intersection:

  • By using the intersection() Method
  • By using the Python Bitwise “&” Operator
  • By using the intersection_update() Method
  • By using the “&=” Operator

Python Set Intersection using intersection() method

  • The intersection() method takes one or more iterables as arguments i.e strings, lists, tuples, etc.
  • The method compares and finds out the common elements among the passed iterables.
  • Finally, a new set as output is created which contains the elements that are common to the iterables.

Note: If any iterable other than set is passed as an argument, then firstly, the iterable object gets converted to a set object and then the intersection is performed on it.

Example:

set1 = {10, 20, 30}
set2 = {30, 3, 9}


output = set1.intersection(set2)

print(output)

Output:

{30}

Python Set Intersection using Bitwise “&” Operator

  • The Python “&” Operator also returns the intersection of elements of two or more sets.
  • The only difference between & operator and intersection() method is that the & operator operates only on set objects while the intersection method can operate on any iterable object like lists, sets, etc.

Example:

set1 = {"Safa", "Aman", "Pooja", "Divya"}


set2 = {"Safa", "Aryan", "Nisha", "Raghav", "Divya"}


Result = set1 & set2


print('Set 1: ',set1)
print('Set 2: ',set2)
print('Set Intersection: ',Result)


Output:

Set 1:  {'Safa', 'Pooja', 'Divya', 'Aman'}
Set 2:  {'Nisha', 'Aryan', 'Raghav', 'Safa', 'Divya'}
Set Intersection:  {'Safa', 'Divya'}

Python Set Intersection using intersection_update() method

The intersection_update() method basically returns the common elements among the iterables and updates the same set/iterable object on which the operation is performed.

Note: It does not create a new set as an output. Instead, it updates the same input set with the result of the intersection operation. See below example to understand better

Example:

set1 = {"Safa", "Aman", "Pooja", "Divya"}


set2 = {"Safa", "Aryan", "Nisha", "Raghav", "Divya"}

print("Set1 before intersection operation: ", set1)
set1.intersection_update(set2)


print('Set Intersection of set1 and set2: ',set1)
print('Updated Set1: ',set1)


Output:

Set1 before intersection operation:  {'Aman', 'Pooja', 'Divya', 'Safa'}
Set Intersection of set1 and set2:  {'Divya', 'Safa'}
Updated Set1:  {'Divya', 'Safa'}

Python Set Intersection using the “&=” Operator

The “&=” operator also returns the intersection among the set objects.

Note: The “&=” operator performs and operates only on set objects. It does not support any other iterable objects such as lists, strings, etc.

Example:

set1 = {"Safa", "Aman", "Pooja", "Divya"}


set2 = {"Safa", "Aryan", "Nisha", "Raghav", "Divya"}

print("Set1 before intersection operation: ",set1)

set1 &= set2

print('Set Intersection of set1 and set2: ',set1)

print("Updated Set1: ", set1)

Output:

Set1 before intersection operation:  {'Divya', 'Safa', 'Pooja', 'Aman'}
Set Intersection of set1 and set2:  {'Divya', 'Safa'}
Updated Set1:  {'Divya', 'Safa'}

Conclusion

Thus, in this article, we have studied and implemented the Python Set Intersection with possible ways.


References