Python Set symmetric_difference_update()


The symmetric_difference_update() method finds the symmetric difference of two sets (non-similar items of both sets) and updates it to the set that calls the method.

Example

A = {'a', 'c', 'd'}
B = {'c', 'd', 'e' }

# updates A with the symmetric difference of A and B A.symmetric_difference_update(B)
print(A) # Output: {a, e}

symmetric_difference_update() Syntax

The syntax of the symmetric_difference_update() method is:

A.symmetric_difference_update(B)

Here, A and B are two sets.


symmetric_difference_update() Parameter

The symmetric_difference_update() method takes a single parameter:

  • B - a set that pairs with set A to find non-similar items of both sets

symmetric_difference_update() Return Value

The symmetric_difference_update() method doesn't return any value.


Example: Python Set symmetric_difference_update()

# create two sets A and B
A = {'Python', 'Java', 'Go'}
B = {'Python', 'JavaScript', 'C' }

print('Original Set A:', A)

# updates A with the symmetric difference of A and B A.symmetric_difference_update(B)
print('Updated Set A:', A)

Output

Original Set A: {'Python', 'Java', 'Go'}
Updated Set A: {'Java', 'C', 'Go', 'JavaScript'}

In the above example, we have used the symmetric_difference_update() method that returns the set with items that are unique in both sets i.e. it removes similar items (intersection) of the set.

Here, set A is calling the method, so the result is updated to set A. Whereas, the set B remains unchanged.


Recommended Reading: Python Set symmetric difference()