symmetric_difference_update(other) changes the set so that it keeps only the items that were in exactly one of the two collections. An item found in both is removed, and an item found only in other is added.
In other words, it toggles every item of other. It does the same job as symmetric_difference(), but overwrites the original set and returns None.
set.symmetric_difference_update() is an instance method of the built-in set type: call it on a set with dot notation, such as lights_on.symmetric_difference_update(...). No import is needed.
Syntax
Pass exactly one iterable, and call it on its own line, because the return value is always None.
python
s.symmetric_difference_update(iterable)
s ^= other_set
Call
What it does
{1, 2, 3}.symmetric_difference_update({3, 4})
Returns None; the set becomes {1, 2, 4}: 3 is removed and 4 is added.
{1, 2}.symmetric_difference_update([2, 3])
Returns None; the set becomes {1, 3}. Any iterable works as the argument.
{1, 2}.symmetric_difference_update({1, 2})
Returns None; the set becomes set(), because every item is in both and cancels out.
s ^= other has the same effect, but needs another set on the right. If you must keep the original set, use symmetric_difference() to get a new set.
Method signatures
python
set.symmetric_difference_update(other)
# returns: None; the set now holds the items in exactly one of the set and other
# raises: TypeError unless exactly one iterable is given
Parameters
Parameter
Description
other
One iterable: set, list, tuple, string, and so on. Repeated items in a list count once, so each item is toggled at most one time.
Return Value
Return Value
Description
None
Always None. The set itself now holds the unshared items; it is empty (set()) if both collections held the same items.
Python Examples
Example 1 — Flip Light Switches on a Control Panel
Items already on are switched off, and new items are switched on, in one call.