Python set.symmetric_difference() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

symmetric_difference(other) builds a new set with the items that are in exactly one of the two collections: in the set or in other, but not in both. Neither original is changed.

Think of it as everything that differs between two groups. It equals the union minus the intersection, and it gives the same answer whichever side you start from.

set.symmetric_difference() is an instance method of the built-in set type: call it on a set with dot notation, such as last_week.symmetric_difference(...). No import is needed.

Syntax

Pass exactly one iterable, and keep the returned set in a variable.

python
changed = s.symmetric_difference(iterable)
changed = s ^ other_set
CallWhat it does
{1, 2, 3}.symmetric_difference({3, 4})Returns {1, 2, 4}: 3 is in both, so it is left out.
{1, 2}.symmetric_difference([2, 3])Returns {1, 3}: any iterable works as the argument.
{1, 2}.symmetric_difference({1}, {2})Raises TypeError: the method takes exactly one argument.

a ^ b is a shortcut for a.symmetric_difference(b), but only between two sets. To change the existing set instead, use symmetric_difference_update().

Method signatures
python
set.symmetric_difference(other)
# returns: a new set with the items in exactly one of the set and other
# raises: TypeError unless exactly one iterable is given

Parameters

ParameterDescription
otherOne iterable: set, list, tuple, string, and so on. Unlike union() and intersection(), you cannot pass several; chain calls or operators instead.

Return Value

Return ValueDescription
setA new set with the items present in only one side. It is empty (set()) when the two collections hold the same items.

Python Examples

Example 1 — Spot What Changed Between Two Schedules

Find the activities that were dropped or added, and split them with the plain difference.

python
last_week = {"gym", "yoga", "swim", "run"}
this_week = {"yoga", "run", "chess", "hike"}

changed = last_week.symmetric_difference(this_week)
print(sorted(changed))
print(sorted(last_week - this_week))
print(sorted(this_week - last_week))

How It Works

  1. Everything that changed. Yoga and run are in both weeks, so they are left out; four activities remain.
  2. Dropped activities. last_week - this_week lists what you did before but not now: gym and swim.
  3. New activities. this_week - last_week lists chess and hike, and together with the dropped ones they make up the full result.

Example 2 — Check the Union-Minus-Intersection Rule

Use a list as the argument, test the rule, and see what a set does against itself.

python
a = {1, 2, 3, 4}
b = [3, 4, 5, 6]

sym = a.symmetric_difference(b)
print(sorted(sym))

both = set(b)
rule = (a | both) - (a & both)
print(sym == rule)
print(sorted(a ^ {1}))
print(a.symmetric_difference(a))

How It Works

  1. A list as the argument. 3 and 4 are in both collections and drop out, leaving 1, 2, 5, and 6.
  2. Union minus intersection. Taking everything and then removing the shared items gives exactly the same set, so the comparison prints True.
  3. The operator form. a ^ {1} removes 1 because it is in both, and nothing new is added.
  4. A set against itself. Every item is in both, so none is left and Python prints set().

Example 3 — Find Mismatched Customer Ids Between Two Systems

Compare a billing set with a CRM list, split the mismatches by side, and try to pass two arguments.

python
billing = {"c1", "c2", "c3", "c7"}
crm = ["c2", "c3", "c4", "c5"]

mismatch = billing.symmetric_difference(crm)
print(len(mismatch))
only_billing = sorted(mismatch & billing)
only_crm = sorted(mismatch.difference(billing))
print(only_billing, only_crm)

try:
    billing.symmetric_difference(["c1"], ["c9"])
except TypeError:
    print("only one argument is allowed")

How It Works

  1. Four ids disagree. c2 and c3 exist in both systems, so c1, c7, c4, and c5 are the mismatches.
  2. Split by side. Intersecting the mismatches with billing gives c1 and c7; subtracting billing leaves c4 and c5.
  3. Two arguments are rejected. symmetric_difference() accepts only one iterable, so the call raises TypeError, which the except block catches.

Frequently Asked Questions

A new set with the items that are in exactly one of the two collections, which means items found in both are left out. The originals are not changed.
Yes for two sets, but the ^ operator needs sets on both sides. The method also accepts a list, tuple, or string as its argument.
difference() keeps only the items from the first set that are missing from the second. symmetric_difference() keeps the unshared items from both sides.
No. It takes exactly one argument and raises TypeError otherwise. Chain the operator instead, as in a ^ b ^ c.
Use symmetric_difference_update() or the ^= operator. They update the original set and return None.

Next: set.symmetric_difference_update()

Keep the items that are in exactly one of the set and the other iterable, in place.

symmetric_difference_update() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful