Skip to main content

Python Set symmetric_difference_update() Function

The Set symmetric_difference_update() method finds the symmetric difference between two sets and updates the set that calls this method.

It modifies the original set by removing items that are present in both sets and inserting the other items, essentially updating the set to contain only the elements that are in either of the sets but not in both.

Syntax​

my_set.symmetric_difference_update(set)

symmetric_difference_update() Parameters​

Python Set symmetric_difference_update() function parameters:

ParameterConditionDescription
setRequiredA set for symmetric difference
note

You can also pass iterables like list, tuple, dictionary or string. In that case, symmetric_difference_update() method first converts the iterables into sets and then computes the symmetric difference.

symmetric_difference_update() Return Value​

Python Set symmetric_difference_update() function does not return any value: it modifies the original set in-place.

Examples​

Example 1: Symmetric Difference of Two Sets with symmetric_difference_update() method​

A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}

A.symmetric_difference_update(B)

print(A) # Output: {'Anna'}

output

{'Anna'}

Example 2: Symmetric Difference between Multiple Sets​

The symmetric_difference_update() method does not allow to compute the symmetric difference between multiple sets.

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

A.symmetric_difference_update(B, C)

output

Traceback (most recent call last):
File "main.py", line 5, in <module>
result = A.symmetric_difference_update(B, C)
TypeError: symmetric_difference_update() takes exactly one argument (2 given)

However, using ^= operator, you can find symmetric difference between multiple sets.

Example 3: Symmetric Difference between a Set and a List​

You can also pass iterables like list: symmetric_difference() first converts the iterables to sets and then computes the symmetric difference in-place.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a List B
B = ['d', 'e', 'f']

A.symmetric_difference_update(B)

print(A) # Output: {'f', 'i', 'u', 'a', 'd', 'o'}

output

{'f', 'i', 'u', 'a', 'd', 'o'}

Example 4: Symmetric Difference between a Set and a Tuple​

You can also pass iterables like tuple: symmetric_difference_update() first converts the iterables to sets and then computes the symmetric difference in-place.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a Tuple B
B = ('d', 'e', 'f')

A.symmetric_difference_update(B)

print(A) # Output: {'f', 'i', 'u', 'a', 'd', 'o'}

output

{'f', 'i', 'u', 'a', 'd', 'o'}

Example 5: Symmetric Difference between a Set and a Dictionary​

You can also pass iterables like dictionary: symmetric_difference_update() first converts the iterables to sets and then computes the symmetric difference.

note

The dictionary keys are used for symmetric_difference_update(), not the values!

For example:

# create a Set A1 and A2
A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}

# create Dictionary B and C
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}

A1.symmetric_difference_update(B)
A2.symmetric_difference_update(C)

print(A1) # Output: {1, 2, 3, 'o', 'a', 'i', 'u', 'e'}
print(A2) # Output: {'o', 'a', 'f', 'i', 'u', 'd'}

output

{1, 2, 3, 'o', 'a', 'i', 'u', 'e'}
{'o', 'a', 'f', 'i', 'u', 'd'}

Example 6: Symmetric Difference between a Set and a String​

You can also pass iterables like string: symmetric_difference_update() first converts the iterables to sets and then computes the symmetric difference.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a String B
B = 'def'

A.symmetric_difference_update(B)

print(A) # Output: {'a', 'f', 'u', 'o', 'i', 'd'}

output

{'a', 'f', 'u', 'o', 'i', 'd'}

Equivalent Operator ^= for symmetric_difference_update()​

Symmetric difference can be performed with the ^= operator as well, providing an alternative to the symmetric_difference_update() method.

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}

# equivalent to: A.symmetric_difference_update(B)
A ^= B

print(A) # Output: {1, 2, 6, 7}

output

{1, 2, 6, 7}

Moreover, you can use the ^= operator with multiple sets:

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

A ^= B ^ C

print(A) # Output: {1, 2, 6, 7}

output

{1, 2, 6, 7}