set.symmetric_difference_update()

The in-place XOR — keep elements found in only one side; drop the shared ones.

Set methodPython 2.6+Live demo
Common call
diff.symmetric_difference_update(other)
Returns
None — set is mutated to the XOR
Replaces
a manual `s = (s - other) | (other - s)` reassignment
Watch out
takes ONE iterable — unlike update / intersection_update / difference_update which take *others
set.symmetric_difference_update(otherotherA single iterable. The set is updated to elements present in exactly one of the two collections. Unlike the other *_update methods, this takes a SINGLE positional argument, not *others.type: iterable · required)
None

Demo

Live evaluation
Try:
Inputs
setsetexisting elements
othersetthe other set (iterable)
Output
{'1', '2', '3'}.symmetric_difference_update({'2', '3', '4'})
[{'1', '2', '3'}, {'2', '3', '4'}]

symmetric_difference_update mutates the set in place. Real Python returns None; the demo shows the RESULTING state so the effect is visible. Elements present in both are REMOVED; elements present in only one are KEPT. Identical sets XOR to the empty set. Empty other means "keep everything in the set" (no change). Unlike the other *_update methods, this takes ONLY ONE iterable.

Parameters

NameTypeRequiredDescription
otheriterableyesA single iterable. The set is updated to elements present in exactly one of the two collections. Unlike the other *_update methods, this takes a SINGLE positional argument, not *others.

Return value

NoneReturns None. The set is mutated in place — becomes the symmetric difference: elements present in EXACTLY ONE of the two collections. Equivalent to `s ^= set(other)` but accepts any iterable.

Common patterns

Toggle membership
For each element in the other iterable — add if absent, remove if present.
flags.symmetric_difference_update({"debug", "trace"})
Detect changes between two snapshots
Symmetric difference is the "what changed" set — added and removed items together.
changed = snapshot_a.copy()
changed.symmetric_difference_update(snapshot_b)
The operator alternative
`^=` is equivalent when the RHS is a set. symmetric_difference_update accepts any iterable.
s ^= other_set                                # requires a set
s.symmetric_difference_update(iterable)        # any iterable

Examples

1. Basic
s = {1, 2, 3} s.symmetric_difference_update({2, 3, 4}) s
Returns
{1, 4}
2. Partial overlap
{"a", "b", "c"}.symmetric_difference_update({"c", "d"})
Returns
{"a", "b", "d"}
3. No overlap = union
{1, 2}.symmetric_difference_update({3, 4})
Returns
{1, 2, 3, 4}
4. Identical sets = empty
{"a"}.symmetric_difference_update({"a"})
Returns
set()
5. Empty other
{1, 2}.symmetric_difference_update([])
Returns
{1, 2}
6. Returns None
{1, 2}.symmetric_difference_update([1])
Returns
None

Pitfalls

1. symmetric_difference_update() returns None — do NOT assign the result
Same rule as every mutation. Assigning the result sets your variable to None.
Assigned None
s = s.symmetric_difference_update(other)
s is now None
Just call it
s.symmetric_difference_update(other)
s is the XOR
2. Takes ONE iterable — unlike its siblings
Alone in the *_update family: it accepts only ONE positional argument. update, intersection_update, and difference_update all accept *others (multiple iterables); symmetric_difference_update does not.
Multi-arg fails
s.symmetric_difference_update(a, b)
TypeError: takes 1 positional argument but 2 were given
Chain calls
s.symmetric_difference_update(a)
s.symmetric_difference_update(b)
iterative XOR
3. Identical sets XOR to empty
When the two sets are identical, everything is shared, so the result is empty. Sometimes forgotten when using XOR as a "combine unique" operation.
Assumed union
{1, 2}.symmetric_difference_update({1, 2})
set() # not {1, 2}
Union for that
s.update(other)
union — every element
4. A string is a sequence of characters
Passing a string XORs each CHARACTER. Wrap in a list to XOR the string itself.
Broken into chars
s = {"h", "hi"}
s.symmetric_difference_update("hi")
{"hi", "i"} # per-char
Wrap it
s.symmetric_difference_update(["hi"])
XOR by whole string

When to use

Use it
  • Toggling membership: add absent, remove present
  • Computing changes between two snapshots
  • "Only in one" queries against a moving target
  • Iterative diff building — apply many small XORs
Reach for something else
  • You need a new set — use `^` or `symmetric_difference()`
  • You want only additions or only removals — separate difference calls
  • Multiple iterables — this method only takes one; call multiple times
  • You need the two halves separately — compute `s - other` and `other - s`

Notes

Complexity
O(len(other)) — one pass over the other iterable
Return
None; the set is mutated in place
CPython impl
Objects/setobject.c :: set_symmetric_difference_update
Memory
May grow or shrink; hash table capacity adjusts
Thread-safe
Not safe under concurrent iteration or mutation

FAQ

For a set on the right, they are equivalent: `s ^= t` calls `s.symmetric_difference_update(t)`. But the method accepts ANY iterable (list, string, generator); the operator requires a set.

History

2.3
set added as a builtin type with symmetric_difference_update().