set.symmetric_difference()
set.symmetric_difference(other) set · Updated March 15, 2026 · Set Methods The .symmetric_difference() method returns a new set containing all elements that are in either of the sets, but not in both. Think of it as the “exclusive or” operation for sets — it gives you everything unique to each set, excluding shared elements.
Syntax
set.symmetric_difference(other)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
other | iterable | — | Any iterable (set, list, tuple, etc.) to compare against. Elements in both sets will be excluded from the result. |
Examples
Basic usage
Find elements that are in either set but not both:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
result = a.symmetric_difference(b)
print(result)
# {1, 2, 5, 6}
Elements 3 and 4 appear in both sets, so they’re excluded. Only 1, 2, 5, and 6 remain.
Simple string example
fruits = {"apple", "banana", "cherry"}
tropical = {"banana", "mango", "pineapple"}
unique_fruits = fruits.symmetric_difference(tropical)
print(unique_fruits)
# {apple, cherry, mango, pineapple}
Order doesn’t matter
The result is the same regardless of which set calls the method:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result1 = set1.symmetric_difference(set2)
result2 = set2.symmetric_difference(set1)
print(result1) # {1, 4}
print(result2) # {1, 4}
# Both are identical
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]
result = numbers.symmetric_difference(more_numbers)
print(result)
# {1, 2, 3, 6, 7}
Using the ^ operator
The caret (^) operator is a convenient shorthand:
a = {1, 2, 3}
b = {2, 3, 4}
# These are equivalent:
result1 = a.symmetric_difference(b)
result2 = a ^ b
print(result1) # {1, 4}
print(result2) # {1, 4}
Common Patterns
Finding differences between user groups
premium_users = {"alice", "bob", "charlie", "david"}
active_users = {"charlie", "david", "eve", "frank"}
# Users in one group but not both
unique_to_premium = premium_users.symmetric_difference(active_users)
print(unique_to_premium)
# {alice, bob, eve, frank}
This pattern is useful for comparing feature adoption, identifying churn, or finding users who only have one type of access.
Detecting changes between versions
old_config = {"host", "port", "timeout", "retry"}
new_config = {"host", "port", "timeout", "retry", "ssl", "proxy"}
changes = old_config.symmetric_difference(new_config)
print(changes)
# {ssl, proxy}
Comparing file lists
folder_a = {"file1.txt", "file2.txt", "file3.txt"}
folder_b = {"file2.txt", "file3.txt", "file4.txt"}
only_in_a = folder_a.symmetric_difference(folder_b)
print(only_in_a)
# {file1.txt, file4.txt}
Finding unpaired items
Track items that appear in one collection but not paired in another:
received = {"item1", "item2", "item3", "item4"}
requested = {"item2", "item3", "item4", "item5"}
mismatches = received.symmetric_difference(requested)
print(mismatches)
# {item1, item5}
symmetric_difference() vs symmetric_difference_update()
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.
Performance
The .symmetric_difference() method:
- Has O(len(s) + len(other)) time complexity
- Creates a new set, leaving the originals unchanged
- Requires hashing each element once
For large sets, consider whether you need the immutable version (this method) or can use the in-place .symmetric_difference_update().
See Also
- set-methods:set-union — combines all elements from multiple sets
- set-methods:set-difference — elements in one set but not another
- set-methods:set-intersection — elements common to both sets