Skip to content
Set Methods

symmetric_difference()

Return a new set with elements in either set, but not both.

set.symmetric_difference(other)

set.symmetric_difference() returns a new set containing elements that exist in either the calling set or other, but not in both (XOR operation).

Equivalent to the ^ operator.

Parameters

NameDescription
otherAn iterable to compare with.

Returns

A new set containing the symmetric difference.

Try it

Run it and change it
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
print(sorted(set_a.symmetric_difference(set_b)))

Worth knowing

  • Operator syntax: set_a ^ set_b computes the exact same symmetric difference between two sets.

Related entries