Python Set Difference with Example – Python Tutorial

6 years ago Lalit Bhagtani 0

In this tutorial, we will learn about different ways of performing difference operation on a given pair of sets in python.
Python Set Difference

Difference of Sets:

In set theory, the difference of two sets A and B, written as A – B is a set which contains all elements of set A that are not in set B. For example :

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

Difference of A & B  :-
A - B = {1, 2, 3}

Python Set Difference

Python Set Difference:

In Python, there are four different ways of performing difference operation on a given pair of sets, they are as follows :

  1. difference Method
  2. – Operator
  3. difference_update Method
  4. -= Operator

difference Method :

This method is used to return the difference of a set and the set of elements from one or more iterable like string, list, set. This method takes arbitrary ( one or more ) number of iterable objects as argument. If iterable other than set is passed, it first converts the iterable object to set object and then perform the union operation on all sets arguments ( passed or converted from iterables ). It returns a newly created set, which contains all elements present in set ( on which difference method is invoked ) that are not in set ( union of passed set or converted from iterables ).

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

Example:

# Python Set Difference # 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 difference method to get A - B and assign it to variable S S = A.difference(B) # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Difference B : ',S) # create list object and assign it to variable L L = [1,4,5,10,10,11,12] # call difference method to get A - L and assign it to variable S S = A.difference(L) # print all the values of set A, B, S and list L print('A : ',A) print('L : ',L) print('A - L : ',S) # create set object and assign it to variable C C = {3,4,5} # call difference method to get A - (B U C) and assign it to variable S S = A.difference(B,C) # print all the values of set A, B, C and S print('A : ',A) print('B : ',B) print('C : ',C) print('A Difference (B Union C) : ',S)

– Operator :

This operator is used to return the difference of two sets just like difference() method. The difference between – operator and difference() 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 >

Example:

# Python Set Difference # 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 A - 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 - B : ',S) # create list object and assign it to variable L L = [1,4,5,10,10,11,12] # use - operator to get A - L and assign it to variable S S = A - L # TypeError is raised as - opearator works with set operands only

difference_update Method :

This method is used to return the difference of a set and the set of elements from one or more iterable like string, list, set. It is very similar to difference() method, with difference is that where difference() method create and return a new set, difference_update() method updates the set on which this method is invoked.

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

Example:

# Python Set Difference # 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 difference_update method to get A - B by updating set A A.difference_update(B) # print all the values of set A print('A Difference B : ',A) # reassign new set to variable A A = {1,2,3,4,5} # create list object and assign it to variable L L = [1,4,5,10,10,11,12] # call difference_update method to get A - L by updating set A A.difference_update(L) # print all the values of set A print('A Difference L : ',A) # reassign new set to variable A A = {1,2,3,4,5} # create set object and assign it to variable C C = {3,4,5} # call difference_update method to get A - (B U C) by updating set A A.difference_update(B,C) # print all the values of set A print('A Difference (B Union C) : ',A)

-= Operator :

This operator is used to return the difference of two sets just like difference_update() method. The difference between -= operator and difference_update() 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 >

Example:

# Python Set Difference # 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 A - B by updating set A A -= B # print all the values of set A print('A - B : ',A) # create list object and assign it to variable L L = [4,5,10,11,12] # use -= operator to get B - L and assign it to variable B B -= L # TypeError is raised as -= opearator works with set operands only

References :-

  1. difference method Docs
  2. difference_update method Docs

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

Previous