Python Set Symmetric Difference with Example – Python Tutorial

6 years ago Lalit Bhagtani 0

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

Symmetric Difference of Sets:

In set theory, the symmetric difference of two sets A and B, written as A Δ B is a set which contains all elements of set A and B that are not in their intersection ( common in both set A and B ). For example :

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

Symmetric Difference of A & B  :-
A Δ B = {1, 2, 3, 6, 7, 8, 9}

Python Set Symmetric Difference

Python Set Symmetric Difference:

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

  1. symmetric_difference Method
  2. ^ Operator
  3. symmetric_difference_update Method
  4. ^= Operator

symmetric_difference Method :

This method is used to return the symmetric difference of a set and the set of elements from the iterable like string, list, set passed as an argument. This method takes one iterable object as argument, if iterable other than set is passed, it first converts the iterable object to set object and then perform the symmetric difference operation. It returns a newly created set, which contains all elements present in set ( on which difference method is invoked ) and in set ( set of iterable passed as an argument ), except the elements present in their intersection.

Syntax : < Set Object >.symmetric_difference( <iterable object> ) : < Set Object >

Example:

# Python Set Symmetric 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 symmetric_difference() and assign the result to variable S S = A.symmetric_difference(B) # print all the values of set A, B and S print('A : ',A) print('B : ',B) print('A Symmetric Difference B : ',S) # create list object and assign it to variable L L = [1,4,5,10,10,11,12] # call symmetric_difference() and assign the result to variable S S = A.symmetric_difference(L) # print all the values of set A, S and list L print('A : ',A) print('L : ',L) print('A Symmetric Difference L : ',S)

^ Operator :

This operator is used to return the symmetric difference of two sets just like symmetric_difference() method. The difference between ^ operator and  symmetric_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 Symmetric 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 and assign the result 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 and assign the result to variable S S = A ^ L # TypeError is raised as ^ opearator works with set operands only

symmetric_difference_update Method :

This method is used to return the symmetric difference of a set and the set of elements from the iterable like string, list, set passed as an argument. It is very similar to symmetric_difference() method, with difference is that where symmetric_difference() method create and return a new set, symmetric_difference_update() method updates the set on which this method is invoked.

Syntax : < Set Object >.symmetric_difference_update( <iterable object> ) 

Example:

# Python Set Symmetric 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 symmetric_difference_update() and assign the result by updating set A A.symmetric_difference_update(B) # print all the values of set A print('A Symmetric 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 symmetric_difference_update() and assign the result by updating set A A.symmetric_difference_update(L) # print all the values of set A print('A Symmetric Difference L : ',A)

^= Operator :

This operator is used to return the symmetric difference of two sets just like symmetric_difference_update() method. The difference between ^= operator and symmetric_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 Symmetric 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 and assign result 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 = [1,4,5,10,10,11,12] # use ^= operator and assign result by updating set A A ^= L # TypeError is raised as ^= opearator works with set operands only

References :-

  1. symmetric_difference method Docs
  2. symmetric_difference_update method Docs

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

Previous