Skip to content

Python Set symmetric_difference_update() Method

How to use Python's symmetric_difference_update() method to modify a set in-place by removing common elements and adding unique ones from another iterable like lists, tuples, or dictionaries.

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() method parameters:

Parameter Condition Description
set Required A set for symmetric difference

symmetric_difference_update() Return Value

Python Set symmetric_difference_update() method 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:

A = {'a', 'e', 'i', 'o', 'u'}

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:

A = {'a', 'e', 'i', 'o', 'u'}

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.

For example:

A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}

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:

A = {'a', 'e', 'i', 'o', 'u'}

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}

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}
Navigation