The symmetric_difference_update()
function in Python is a method used to update a set by removing the elements that are present in both sets, and inserting the elements from the other set that are not common. This operation is also known as symmetric difference or exclusive OR (XOR) operation between sets.
Parameter Values
Parameter | Description |
---|---|
iterable | A |
Return Values
The symmetric_difference_update()
method returns None
.
How to Use symmetric_difference_update()
in Python
The symmetric_difference_update()
method updates a set with the symmetric difference of itself and another set.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set1.symmetric_difference_update(set2)
print(set1) # Output: {1, 2, 3, 6, 7, 8}
If the two sets don't have any elements in common, symmetric_difference_update()
will result in the union of the two sets.
set1 = {1, 2, 3}
set2 = {4, 5}
set1.symmetric_difference_update(set2)
print(set1) # Output: {1, 2, 3, 4, 5}
The symmetric_difference_update()
method modifies the original set in place and does not return a new set.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result = set1.symmetric_difference_update(set2)
print(result) # Output: None