all Python 3Sets (set)function
set.symmetric_difference
set theory
Return elements in exactly one of the two sets (symmetric difference). Operator: ^.
Syntax
s.symmetric_difference(other)
Unlike difference() and intersection(), this one takes exactly one argument — you cannot pass several collections at once. The result does not depend on operand order, which makes it the direct way to ask how two collections differ at all, without deciding in advance which side has the extras; the ^ operator, as usual, needs sets on both sides.
Examples
print(sorted({1, 2, 3}.symmetric_difference({2, 3, 4}))) # → [1, 4]
print(sorted({1, 2, 3} ^ {2, 3, 4})) # → [1, 4]
print(sorted({1, 2}.symmetric_difference([2, 3]))) # → [1, 3]
print({1, 2}.symmetric_difference({1, 2})) # → set()
print(sorted({1, 2, 3}.difference({2, 3, 4}))) # → [1]
print({1, 2} ^ [2, 3]) # → TypeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy