Python Set symmetric_difference_update() Method

Updates the set by keeping only elements found in either set, but not in both

Usage

The symmetric_difference_update() method updates the set by keeping only elements found in either set, but not in both.

If you don’t want to update the original set, use symmetric_difference() method.

The symmetric difference is actually the union of the two sets, minus their intersection.

Syntax

set.symmetric_difference_update(set)

Python set symmetric_difference_update() method parameters
ParameterConditionDescription
setRequiredA set to find difference in

Basic Example

# Update A by adding items from B, except common items
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

A.symmetric_difference_update(B)
print(A)
# Prints {'blue', 'orange', 'green', 'yellow'}
Set symmetric difference

Equivalent Operator ^=

You can achieve the same result by using the ^= augmented assignment operator.

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

A ^= B
print(A)
# Prints {'blue', 'orange', 'green', 'yellow'}