## Python Set Intersection with Example – Python Tutorial

###### 1 year ago Lalit Bhagtani 0

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

**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 :**

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

- intersection Method
- & Operator
- intersection_update Method
- &= 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:**

[datacamp_exercise lang=”python” height=”450″ min-height=”200″ max-height=”500″]

[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]

[datacamp_sample_code]

# 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)

[/datacamp_sample_code]

[datacamp_solution]

[/datacamp_solution]

[/datacamp_exercise]

**& 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:**

[datacamp_exercise lang=”python” height=”450″ min-height=”200″ max-height=”500″]

[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]

[datacamp_sample_code]

# 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

[/datacamp_sample_code]

[datacamp_solution]

[/datacamp_solution]

[/datacamp_exercise]

**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:**

[datacamp_exercise lang=”python” height=”450″ min-height=”200″ max-height=”500″]

[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]

[datacamp_sample_code]

# 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)

[/datacamp_sample_code]

[datacamp_solution]

[/datacamp_solution]

[/datacamp_exercise]

**&= 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:**

[datacamp_pre_exercise_code]

[datacamp_sample_code]

# 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

[/datacamp_sample_code]

[datacamp_solution]

[/datacamp_solution]

[/datacamp_exercise]

**References :-**

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.