Python Set symmetric_difference_update() method
Python Set symmetric_difference_update() method
The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set.
Syntax
set.symmetric_difference_update(set1)
Parameter
set- This parameter represents the first set that is used for checking the matches.
set1- This parameter represents the second set that is also used for checking the matches.
Return
None
Example 1
# Python program explaining
# the set.symmetric_difference_update() method
#initializing set1
set1 = {"mango", "banana", "apple","tomato"}
print("Set 1:",set1)
# initializing set2
set2 = {"potato", "spinach","Carrot", "apple"}
print("Set 2:",set2)
# updates the original set by removing items that are present in both sets
set1.symmetric_difference_update(set2)
print("\nAfter the symmetric_difference_update() method...\n")
# printing the updated new set
print("Updated Set 1:",set1)
# no change occurs at second set
# the second set remains the same
print("Set 2:",set2)
Output
Set 1: {'banana', 'apple', 'tomato', 'mango'}
Set 2: {'spinach', 'potato', 'Carrot', 'apple'}
After the symmetric_difference_update() method...
Updated Set 1: {'potato', 'tomato', 'mango', 'banana', 'spinach', 'Carrot'}
Set 2: {'spinach', 'potato', 'Carrot', 'apple'}
Example 2
# Python program explaining
# the set.symmetric_difference_update() method
# initializing the set1
set1 = {1,2,3,4,5,6}
# initializing the set2
set2 = {2,7,8,6,5}
# updates the original set by removing items that are present in both sets
set1.symmetric_difference_update(set1) # passing same sets in both the arguments
# printing the updated set
# will return set() as it deletes the common values.
print("Updated Set 1:", set1)
# no change in set2
print("Set 2:",set2)
Output
Updated Set 1: set()
Set 2: {8, 2, 5, 6, 7}
