Python Set Intersection with Example – Python Tutorial

5 months ago Lalit Bhagtani 0

In this tutorial, we will learn about different ways of performing intersection operation on two or more sets in python.
Python Set Intersection

Intersection of Sets:

In set theory, the intersection of two or more sets is the set which contains the elements that are common to all the sets. For example :

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8, 9}

Intersection of A & B :-
A ∩ B = {4, 5}

Python Set Intersection

Python Set Intersection :

In Python, there are four different ways of performing intersection operation on collection of sets, they are as follows :

  1. intersection Method
  2. & Operator
  3. intersection_update Method
  4. &= Operator

intersection Method :

This method is used to return the intersection of a set and the set of elements from one or more iterable like string, list, set. This method takes arbitrary number of iterable objects as argument, which means one or more iterable can be passed. If iterable other than set is passed, it first converts the iterable object to set object and then perform the intersection operation. It returns a newly created set, which contains elements ( distinct ) common in all the iterables.

Syntax : < Set Object >.intersection( *<iterable object> ) : < Set Object >

Example:

# Python Set Intersection # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # call intersection method to get intersection of set A and B and assign it to variable S S = A.intersection(B) # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Intersection B : ',S) # create list object and assign it to variable L L = [4,5,10,11,12] # call intersection method to get intersection of set A, B and list L and assign it to variable S S = A.intersection(B,L) # print all the values of set A, B, S and list L print('A : ',A) print('B : ',B) print('L : ',L) print('A Intersection B Intersection L : ',S)

& Operator :

This operator is used to return the intersection of two or more sets just like intersection() method. The difference between & operator and intersection() method is that, the former can work only with set objects while latter can work with any iterable objects like list, string, set.

Syntax : < Set Object 1 > &  < Set Object 2 > & < Set Object 2 >… : < Set Object >

Example:

# Python Set Intersection # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # use & operator to get intersection of set A and B and assign it to variable S S = A & B # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Intersection B : ',S) # create set object and assign it to variable C C = {4,5,6,7,8,9,10,11,12} # use & operator to get intersection of set A and B and C and assign it to variable S S = A & B & C # print all the values of set A, B, C and S print('A : ',A) print('B : ',B) print('C : ',C) print('A Intersection B Intersection C : ',S) # create list object and assign it to variable L L = [4,5,10,11,12] # use & operator to get intersection of set A and list L S = A & L # TypeError is raised as & opearator works with set operands only

intersection_update Method :

This method is used to return the intersection of a set and the set of elements from one or more iterable. It is very similar to intersection() method, with difference is that where intersection() method create and return a new set, intersection_update() method updates the set on which this method is called with all the distinct elements common in all the iterables.

Syntax : < Set Object >.intersection_update( *<iterable object> ) 

Example:

# Python Set Intersection # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # call intersection_update method to get intersection of set A and B by updating set A A.intersection_update(B) # print all the values of set A print('A Intersection B : ',A) # create list object and assign it to variable L L = [4,5,10,11,12] # call intersection_update method to get intersection of set A, B and list L by updating set A A.intersection_update(B,L) # print all the values of set A print('A Intersection B Intersection L : ',A)

&= Operator :

This operator is used to return the intersection of two or more sets just like intersection_update() method. The difference between &= operator and intersection_update() method is that, the former can work only with set objects while latter can work with any iterable.

Syntax : < Set Object 1 > &=  < Set Object 2 > & < Set Object 2 >… 

Example:

# Python Set Intersection # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable B B = {4,5,6,7,8,9} # use &= operator to get intersection of set A and B by updating set A A &= B # print all the values of set A print('A Intersection B : ',A) # create set object and assign it to variable A A = {1,2,3,4,5} # create set object and assign it to variable C C = {4,5,6,7,8,9,10,11,12} # use &= operator to get intersection of set A, B and C by updating set A A &= B & C # print all the values of set A print('A Intersection B Intersection C : ',A) # create list object and assign it to variable L L = [4,5,10,11,12] # use &= operator to get intersection of set A and list L A &= L # TypeError is raised as &= opearator works with set operands only

References :-

  1. intersection method Docs
  2. intersection_update method Docs

That’s all for Python Set Intersection with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Previous