Symmetric difference in Python returns elements that are in either set, but not in both. Use a.symmetric_difference(b) or a ^ b. For one-sided removal only, see set difference; for adding members first, see Python set add.
a.symmetric_difference(b) highlights the elements that are unique to each set—present on one side or the other, but not shared. Python's official docs define set.symmetric_difference(other) and set ^ other the same way. The method accepts any iterable; the ^ operator requires set operands.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Python symmetric difference quick reference
| Task | Use |
|---|---|
| Symmetric difference using method | a.symmetric_difference(b) |
| Symmetric difference using operator | a ^ b |
| Update original set | a.symmetric_difference_update(b) |
| Update original set using operator | a ^= b |
| Use list/tuple as second input | a.symmetric_difference(list_or_tuple) |
Use ^ with list/tuple |
Convert to set first |
| Difference, not symmetric difference | a.difference(b) or a - b |
| Sort result for display | sorted(a ^ b) |
What is symmetric difference in Python?
Symmetric difference returns elements that are in set A or set B, but not in both. Shared elements are removed from the result.
- The result is a new set unless you use
symmetric_difference_update()or^=. - Sets are unordered, so do not rely on display order—use
sorted()when you need a stable list.
Formula:
A symmetric difference B = (A − B) ∪ (B − A)
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A.symmetric_difference(B))
print((A - B) | (B - A))Both lines produce {1, 2, 5, 6} (order may vary when printed).
Python symmetric_difference() syntax
result = set_a.symmetric_difference(set_b)- Returns a new set
- Does not modify the original set
- The argument can be another set or any iterable (list, tuple, string, and so on)
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
result = A.symmetric_difference(B)
print(result)
print(A)The result is {1, 2, 5, 6}; A stays {1, 2, 3, 4}.
Python symmetric difference using ^ operator
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A ^ B)Same result as the method: {1, 2, 5, 6}.
Warning: ^ also means bitwise XOR for integers (5 ^ 3). With sets, it means symmetric difference—the operand types decide the meaning.
symmetric_difference() vs ^ operator
| Feature | symmetric_difference() |
^ operator |
|---|---|---|
| Readability | More explicit | Shorter |
| Accepts any iterable | Yes | No; expects sets |
| Returns new set | Yes | Yes |
| Modifies original set | No | No |
| Best for beginners | Yes | Sometimes |
| Best for concise set expressions | Sometimes | Yes |
Use symmetric_difference() when readability matters or the second value may be a list or tuple. Use ^ when both values are already sets and the expression is short.
Python set difference vs symmetric difference
| Operation | Meaning | Example with A={1,2,3,4}, B={3,4,5,6} |
|---|---|---|
a - b / a.difference(b) |
Items in a but not in b | {1, 2} |
b - a / b.difference(a) |
Items in b but not in a | {5, 6} |
a ^ b / a.symmetric_difference(b) |
Items in a or b, but not both | {1, 2, 5, 6} |
differenceis one-sided—it depends on operand order.symmetric differenceis two-sided—unique values from both sets.a - bis not always the same asb - a.a ^ bis the same asb ^ a.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A - B)
print(B - A)
print(A ^ B)
print(B ^ A)You get {1, 2}, {5, 6}, and the same symmetric result both ways.
Symmetric difference with multiple sets
You can chain ^ with more than two sets:
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
C = {5, 6, 7, 8}
print(A ^ B ^ C)The result is {1, 2, 7, 8}.
With more than two sets, chained symmetric difference keeps elements that appear in an odd number of sets—not simply “values that appear in exactly one set.” For example, 5 appears in both B and C (twice), so it cancels out. Most beginner tasks involve two sets; chain only when you understand that parity rule.
symmetric_difference_update() in Python
| Method | Modifies original set? |
|---|---|
a.symmetric_difference(b) |
No |
a ^ b |
No |
a.symmetric_difference_update(b) |
Yes |
a ^= b |
Yes |
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
A.symmetric_difference_update(B)
print(A)A becomes {1, 2, 5, 6}. Use update forms only when you intend to change the left set in place.
Symmetric difference with lists and tuples
The method accepts iterables directly:
A = {1, 2, 3, 4}
list_b = [3, 4, 5, 6, 5]
print(A.symmetric_difference(list_b))
print(A.symmetric_difference((3, 4, 5, 6)))Both return {1, 2, 5, 6}. Duplicate 5 values in the list collapse when treated as a set.
For ^, convert first:
A = {1, 2, 3, 4}
list_b = [3, 4, 5, 6]
print(A ^ set(list_b))Symmetric difference with strings
A string is an iterable of characters:
print(set("abc").symmetric_difference("bcd"))The result is {'a', 'd'}—character-level comparison, not whole words.
For word-level comparison, split first:
text_a = "alpha beta gamma"
text_b = "beta delta"
words_a = set(text_a.split())
words_b = set(text_b.split())
print(words_a.symmetric_difference(words_b))You get words that appear in one string but not both (order not guaranteed in the set display).
Practical examples of symmetric difference
Users in only one system:
system_a = {"u1", "u2", "u3", "u4"}
system_b = {"u3", "u4", "u5", "u6"}
only_one = system_a ^ system_b
print(sorted(only_one))Compare two ID lists:
batch_1 = {101, 102, 103}
batch_2 = [103, 104, 105]
mismatch = batch_1.symmetric_difference(batch_2)
print(mismatch)Registered vs attended (not both):
registered = {101, 102, 103, 104}
attended = {103, 104, 105, 106}
print(registered ^ attended)Different lines between two small texts (simulated without files):
lines_1 = {"error: timeout", "info: started", "warn: retry"}
lines_2 = {"info: started", "error: denied", "debug: trace"}
print(lines_1 ^ lines_2)Each example returns IDs or lines unique to one side or the other.
Summary
Symmetric difference returns elements in either set, but not both. Use symmetric_difference() for readability and iterable inputs. Use ^ for short set-to-set expressions. Use symmetric_difference_update() or ^= to modify the original set. Use difference() or - when you only want items from one set that are missing in another.
Useful references
- Python set operations
- Python sets guide — construction, uniqueness, and set methods

