Skip to main content

symmetric_difference_update()

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 set or any other iterable whose elements will be used to perform the symmetric difference update with the original set.

Return Values

The symmetric_difference_update() method returns None.

How to Use symmetric_difference_update() in Python

Example 1:

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}
Example 2:

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}
Example 3:

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