Python Set symmetric_difference() Method

set.symmetric_difference() returns the values that appear in exactly one of two sets.

This is useful when you want to compare two sets and keep only the non-matching values.

Use it when:

  • you want items in one set or the other
  • you do not want items that appear in both sets
  • you need a new set without changing the originals

Quick answer

a = {1, 2, 3}
b = {3, 4, 5}

result = a.symmetric_difference(b)
print(result)  # {1, 2, 4, 5}

Use this when you want values that are in one set or the other, but not in both.

What symmetric_difference() does

set.symmetric_difference():

  • returns a new set
  • keeps items that are in only one of the two sets
  • removes items that exist in both sets
  • does not change the original set

If you are new to sets, see Python sets explained or creating a set in Python.

Basic syntax

set1.symmetric_difference(set2)

Notes:

  • set1 must be a set
  • set2 can be any iterable
  • the result is always a set
  • the most common use is comparing two sets

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

result = set1.symmetric_difference(set2)
print(result)

Output:

{1, 2, 4, 5}

What the result means

The result includes:

  • items only in the first set
  • items only in the second set

The result does not include:

  • items shared by both sets

For example:

  • first set: {1, 2, 3}
  • second set: {3, 4, 5}

The value 3 is in both sets, so it is removed from the result.

The output is:

{1, 2, 4, 5}

Also remember:

  • sets are unordered
  • Python does not guarantee the printed order of set items

Simple example

Here is a small example with one shared value:

colors1 = {"red", "blue", "green"}
colors2 = {"green", "yellow"}

result = colors1.symmetric_difference(colors2)

print(result)

Output:

{'red', 'blue', 'yellow'}

What happened here:

  • "green" is in both sets, so it is excluded
  • "red" and "blue" are only in colors1, so they stay
  • "yellow" is only in colors2, so it stays

symmetric_difference() vs difference()

These two methods are similar, but they do not do the same thing.

difference():

  • keeps items only from the first set
  • ignores unique items from the second set

symmetric_difference():

  • keeps unique items from both sets
  • removes shared items

Example:

a = {1, 2, 3}
b = {3, 4, 5}

print(a.difference(b))            # {1, 2}
print(a.symmetric_difference(b))  # {1, 2, 4, 5}

Use:

  • difference() for a one-sided comparison
  • symmetric_difference() for a two-sided comparison

symmetric_difference() vs union()

union() combines both sets.

symmetric_difference() only keeps non-shared values.

Example:

a = {1, 2, 3}
b = {3, 4, 5}

print(a.union(b))                 # {1, 2, 3, 4, 5}
print(a.symmetric_difference(b))  # {1, 2, 4, 5}

Difference:

  • union() includes everything
  • symmetric_difference() excludes values found in both sets

Use:

  • union() when overlap does not matter
  • symmetric_difference() when you want non-matching values only

Using the ^ operator

For sets, the ^ operator gives the same result as symmetric_difference().

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.symmetric_difference(set2))
print(set1 ^ set2)

Possible output:

{1, 2, 4, 5}
{1, 2, 4, 5}

Both forms:

  • return a new set
  • do not change the original sets

For beginners, the method form is often easier to read because the name explains what it does.

Common beginner mistakes

Here are some common problems when using symmetric_difference():

  • Expecting sorted output
    Sets are unordered, so the result may print in a different order than you expect.
  • Thinking the original set is changed
    symmetric_difference() returns a new set. It does not modify the original set.
  • Confusing it with difference()
    difference() only keeps items from the first set. symmetric_difference() keeps unique items from both sets.
  • Forgetting that sets remove duplicates
    A set only stores unique values. If your original data has repeated values, they will appear only once.

Common causes of confusion include:

  • using symmetric_difference() when union() is actually needed
  • comparing lists instead of converting them to sets first
  • assuming repeated values will appear multiple times in the result
  • being confused by output order

If you are starting with a list and want unique values first, see how to remove duplicates from a list in Python.

Useful checks while debugging:

print(set1)
print(set2)
print(set1.symmetric_difference(set2))
print(set1 ^ set2)
print(type(set1))
print(type(set2))

FAQ

Does symmetric_difference() change the original set?

No. It returns a new set and leaves the original set unchanged.

What is the difference between symmetric_difference() and difference()?

difference() keeps items only from the first set. symmetric_difference() keeps items that appear in exactly one of the two sets.

Can I use symmetric_difference() with lists?

The method is for sets. Convert a list to a set first if needed.

Example:

list1 = [1, 2, 3]
list2 = [3, 4, 5]

result = set(list1).symmetric_difference(set(list2))
print(result)

Output:

{1, 2, 4, 5}

Why does the output order look random?

Sets are unordered, so Python does not guarantee item order.

See also

If you are learning set methods, compare difference(), union(), and symmetric_difference() side by side. That makes it much easier to choose the right one for real tasks.