set.symmetric_difference_update()

set.symmetric_difference_update(other)
Returns: None · Updated March 15, 2026 · Set Methods
sets methods in-place-operation

The .symmetric_difference_update() method updates a set in-place, keeping only elements that are in either set but not in both. Unlike .symmetric_difference() which returns a new set, this method modifies the original.

Syntax

set.symmetric_difference_update(other)

Parameters

ParameterTypeDefaultDescription
otheriterableAny iterable (set, list, tuple, etc.) to compare against. Elements appearing in both sets will be removed.

Examples

Basic usage

Modify a set to contain only elements unique to each:

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

a.symmetric_difference_update(b)
print(a)
# {1, 2, 5, 6}

Elements 3 and 4 appear in both sets, so they’re removed. Only 1, 2, 5, and 6 remain in the original set a.

Simple string example

fruits = {"apple", "banana", "cherry"}
tropical = {"banana", "mango", "pineapple"}

fruits.symmetric_difference_update(tropical)
print(fruits)
# {apple, cherry, mango, pineapple}

Order doesn’t matter

The operation works the same regardless of which set you modify:

set1 = {1, 2, 3}
set2 = {2, 3, 4}

set1.symmetric_difference_update(set2)
print(set1)  # {1, 4}

Using with lists or other iterables

The parameter doesn’t have to be a set:

numbers = {1, 2, 3, 4, 5}
more_numbers = [4, 5, 6, 7]

numbers.symmetric_difference_update(more_numbers)
print(numbers)
# {1, 2, 3, 6, 7}

Using the ^= operator

The caret-equals (^=) operator is a convenient shorthand:

a = {1, 2, 3}
b = {2, 3, 4}

# These are equivalent:
a.symmetric_difference_update(b)
a ^= b

print(a)  # {1, 4}

Common Patterns

Tracking live changes

Keep a running set of items that have changed state:

active_sessions = {"user1", "user2", "user3", "user4"}
new_sessions = {"user2", "user4", "user5", "user6"}

# Update to track only new changes
active_sessions.symmetric_difference_update(new_sessions)
print(active_sessions)
# {user1, user3, user5, user6}

This pattern is useful for real-time dashboards, tracking presence, or managing state changes.

Updating configuration

Track configuration keys that have changed between versions:

old_config = {"host", "port", "timeout", "retry"}
new_config = {"host", "port", "timeout", "retry", "ssl", "proxy"}

old_config.symmetric_difference_update(new_config)
print(old_config)
# {ssl, proxy}

File comparison

Track which files have changed between versions:

version1 = {"file1.txt", "file2.txt", "file3.txt"}
version2 = {"file2.txt", "file3.txt", "file4.txt"}

version1.symmetric_difference_update(version2)
print(version1)
# {file1.txt, file4.txt}

Managing inventory

Track items with discrepancies between expected and actual:

expected_items = {"item1", "item2", "item3", "item4"}
actual_items = {"item2", "item3", "item4", "item5"}

expected_items.symmetric_difference_update(actual_items)
print(expected_items)
# {item1, item5}

symmetric_difference_update() vs symmetric_difference()

colors = {"red", "blue"}
new_colors = {"blue", "green"}

# symmetric_difference() - returns NEW set, original unchanged
result = colors.symmetric_difference(new_colors)
print(colors)  # {red, blue} - unchanged
print(result)  # {red, green} - new set

# symmetric_difference_update() - modifies original
colors = {"red", "blue"}
colors.symmetric_difference_update(new_colors)
print(colors)  # {red, green} - modified

Use .symmetric_difference() when you need to preserve the original data. Use .symmetric_difference_update() when you want to modify the original set in place and save memory by not creating a new set.

Performance

The .symmetric_difference_update() method:

  • Has O(len(s) + len(other)) time complexity
  • Modifies the set in-place, avoiding allocation of a new set
  • Requires hashing each element once

This method is more memory-efficient than .symmetric_difference() when you don’t need to keep the original set, as it updates rather than creates.

See Also