Python Set symmetric_difference() Method
a.symmetric_difference(b) returns a new set of elements that are in a or in b but not in both. It is the same as a ^ b.Think of it as "what is different between these two groups, in either direction". If one shop lists some products and another shop lists others, the symmetric difference contains the products only one of them sells. It is one of the core set operations.
It equals the union minus the intersection, and also the two one-sided differences combined.
| Syntax | a.symmetric_difference(b) or a ^ b |
|---|---|
| Parameters | Exactly one iterable |
| Returns | A new set |
| Changes the original? | No (symmetric_difference_update() does) |
| Time complexity | O(len(a) + len(b)) |
How set.symmetric_difference() works
Elements that both sets share are dropped, and everything else from both sets is kept. The method takes one argument, unlike union() and intersection(), which accept several.
shop_a = {"pen", "book", "ruler"}
shop_b = {"book", "bag"}
print(sorted(shop_a.symmetric_difference(shop_b)))
print(sorted(shop_a ^ shop_b))Symmetric difference as union minus intersection
These three expressions always give the same set, which is handy for understanding what ^ does.
a = {1, 2, 3}
b = {3, 4}
print(sorted(a ^ b))
print(sorted((a | b) - (a & b)))
print(sorted((a - b) | (b - a)))Which set operation should I use?
| Question you are asking | Method | Operator | For {1, 2, 3} and {3, 4} |
|---|---|---|---|
| Everything in either set? | union() | | | {1, 2, 3, 4} |
| Only what is shared? | intersection() | & | {3} |
| In the first but not the second? | difference() | - | {1, 2} |
| In exactly one of them? | symmetric_difference() | ^ | {1, 2, 4} |
Examples you can run
Press Run, then edit the code and run it again.
Products sold by only one shop
book is sold by both shops so it is dropped.
shop_a = {"pen", "book", "ruler"}
shop_b = {"book", "bag"}
only_one = shop_a.symmetric_difference(shop_b)
print(sorted(only_one))Expected output['bag', 'pen', 'ruler']
Compare two tag lists
The argument can be a list. Identical collections give an empty set, which makes this a quick equality check.
old = {"python", "sets"}
new = ["python", "sets"]
print(old.symmetric_difference(new))
print(old.symmetric_difference(["python", "dicts"]) == {"sets", "dicts"})Expected outputset()
True
Common mistakes
Passing several arguments
✗ Wrong
a = {1, 2}
a.symmetric_difference({2, 3}, {4})✓ Right
a = {1, 2}
print(sorted(a ^ {2, 3} ^ {4}))Chain the operator instead: a ^ b ^ c. Be aware that this keeps elements present in an odd number of the sets.
Confusing ^ with exponent
✗ Wrong
print({1, 2} ^ 3)✓ Right
print(sorted({1, 2} ^ {3}))On sets, ^ is symmetric difference. Use ** for powers of numbers, and give ^ another set.
Python Set symmetric_difference() Method: questions people ask
What does ^ do on Python sets?
It returns the symmetric difference: elements that are in exactly one of the two sets.
What is the difference between difference() and symmetric_difference()?
difference() keeps only items of the first set that are missing from the other, while symmetric_difference() keeps the unmatched items from both sides.
How do I find items that are not common to two lists?
Convert them to sets and use set(a) ^ set(b).
Keep learning
Step-by-step lessons:
Python Set OperationsPython Sets
Related:
Python Set difference() MethodPython Set union() MethodPython Set intersection() Method
More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set intersection() MethodPython Set difference() MethodPython Set update() Method See all →