Symmetric_difference Python set ()

Python Methods and Functions


Syntax :

 set1_name.symmetric_difference (set2_name) 

Parameters:
Only one set is required as a parameter. If passed a list, tuple, or dictionary, it converts it to a set and performs the task.

Return value:

 Returns a set which is the symmetric difference between the two sets. 

Working code for symbric_difference ():

# Python program to demonstrate usage
# of symbric_difference () method

 

 

list1 = [ 1 , 2 , 3

list2 = [ 2 , 3 , 4

list3 [ 3 , 4 , 5

 
# Convert list to sets

set1 = set (list1) 

set2 = set (list2) 

 
# Prints symmetrical difference when
# set is passed as a parameter

print ( set1.symmetric_difference (set2)) 

 
# Prints symmetric differences y when the list
# is passed as a parameter by converting it to a set

print (set2.symmetric_difference (list3))

Output:

 {1, 4} {2, 5} 




Tutorials