Python Symmetric Difference

Learn symmetric difference in Python using set.symmetric_difference() and the ^ operator. See examples, difference vs symmetric difference, multiple sets, lists, tuples, and common mistakes.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python Symmetric Difference

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)

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

print(A.symmetric_difference(B))
print((A - B) | (B - A))
Output

Both lines produce {1, 2, 5, 6} (order may vary when printed).


Python symmetric_difference() syntax

python
result = set_a.symmetric_difference(set_b)
Output
  • 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)
python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

result = A.symmetric_difference(B)
print(result)
print(A)
Output

The result is {1, 2, 5, 6}; A stays {1, 2, 3, 4}.


Python symmetric difference using ^ operator

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

print(A ^ B)
Output

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}
  • difference is one-sided—it depends on operand order.
  • symmetric difference is two-sided—unique values from both sets.
  • a - b is not always the same as b - a.
  • a ^ b is the same as b ^ a.
python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A - B)
print(B - A)
print(A ^ B)
print(B ^ A)
Output

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:

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

print(A ^ B ^ C)
Output

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
python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

A.symmetric_difference_update(B)
print(A)
Output

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:

python
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)))
Output

Both return {1, 2, 5, 6}. Duplicate 5 values in the list collapse when treated as a set.

For ^, convert first:

python
A = {1, 2, 3, 4}
list_b = [3, 4, 5, 6]

print(A ^ set(list_b))
Output

Symmetric difference with strings

A string is an iterable of characters:

python
print(set("abc").symmetric_difference("bcd"))
Output

The result is {'a', 'd'}—character-level comparison, not whole words.

For word-level comparison, split first:

python
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))
Output

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:

python
system_a = {"u1", "u2", "u3", "u4"}
system_b = {"u3", "u4", "u5", "u6"}

only_one = system_a ^ system_b
print(sorted(only_one))
Output

Compare two ID lists:

python
batch_1 = {101, 102, 103}
batch_2 = [103, 104, 105]

mismatch = batch_1.symmetric_difference(batch_2)
print(mismatch)
Output

Registered vs attended (not both):

python
registered = {101, 102, 103, 104}
attended = {103, 104, 105, 106}

print(registered ^ attended)
Output

Different lines between two small texts (simulated without files):

python
lines_1 = {"error: timeout", "info: started", "warn: retry"}
lines_2 = {"info: started", "error: denied", "debug: trace"}

print(lines_1 ^ lines_2)
Output

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


Frequently Asked Questions

1. What does symmetric difference mean in Python?

It returns elements that are in either set but not in both. Common elements are excluded from the result.

2. What is the difference between difference() and symmetric_difference()?

difference() returns items in the first set that are not in the second. symmetric_difference() returns unique items from both sides—elements in either set but not in both.

3. Can symmetric_difference() accept a list or tuple?

Yes. The method form accepts any iterable and converts it to a set. The ^ operator requires both operands to be sets.

4. Does a ^ b change the original sets?

No. a ^ b and a.symmetric_difference(b) return a new set. Use symmetric_difference_update() or a ^= b to modify the left set in place.

5. Is symmetric difference commutative in Python?

Yes. a ^ b gives the same result as b ^ a, and the method behaves the same if you swap the operands.

6. What does a.symmetric_difference(b) highlight in quiz-style questions?

Elements that are in either set but not in both—in other words, values unique to each set after removing shared items.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …