Symmetric Difference of Python Sets

Introduction

The symmetric difference of sets is an operation that extracts all elements that appear in only one set. If we have two sets A and B, their symmetric difference will include all elements in A that are not in B, together with all elements in B that are not in A. Symmetric difference of Python sets, can be achieved using the symmetric_difference() instance method, the symmetric_difference() class method, and the symmetric difference operator, ^, i.e. the caret character. Each of those options provides a slightly different capability and convenience. They work in a similar way to the options provided for the set union, intersection, and difference, and familiarity with those makes learning about symmetric difference rather easy.

Using the symmetric_difference() Instance Method

The symmetric_difference() instance method of a set object is used to extract elements that are unique to the set and one other iterable, excluding common elements. This creates a new set containing those values that are present in only one of the objects.

Note that unlike other set operation methods such as union(), symmetric_difference() will only accept one other argument to perform its operation and no more.

The example below creates the symmetric difference of a set object and a list.

Example:

my_set = {1, 2, 3}
my_list = [1, 4, 5]
symmetric_difference_set = my_set.symmetric_difference(my_list)
print(symmetric_difference_set)

Output:

{2, 3, 4, 5}

In this example, the symmetric_difference() method is used to find elements unique to either my_set or my_list. Since the element 1 is common, it is excluded. The rest of the elements appear in either my_set or my_list, but not both, resulting in the set {2, 3, 4, 5}.

Using the Symmetric Difference Operator ^

The symmetric difference operator, ^, is an alternative way to creating a symmetric difference. Unlike the symmetric_difference() method, it can only be used with proper set objects and not any other iterable type. Using ^ with iterable objects that are not sets results in an error. Also unlike the symmetric_difference() method, the ^ operator can be used with two or more sets to extract all elements that are in only one of those sets.

Example:

set1 = {1, 2, 3}
set2 = {1, 4, 5}
set3 = {2, 4, 6}
symmetric_difference_set = set1 ^ set2 ^ set3
print(symmetric_difference_set)

Output:

{3, 5, 6}

In this example, the symmetric difference of set1, set2 and set3 is generated using the ^ operator. The elements 1, 2, and 4 appear in more than one set. This leaves the symmetric difference to be {3, 5, 6}, a set that contains the elements that are in only one.

Using the symmetric_difference() Class Method

Similar to the symmetric_difference() instance method, the symmetric_difference() class method allows you to find the symmetric difference between a set and one other iterable object.

It is a class method of the set class, and you call it with a set. prefix.

The difference between the symmetric_difference() instance method and symmetric_difference() class method is that the instance method is called on a specific set, whereas the class method includes the set as its first parameter. Following the first argument, which must be a set, the second argument can be an object of any iterable type.

Example:

my_set = {1, 2, 3}
my_lst = [1, 4, 5]
symmetric_difference_set = set.symmetric_difference(my_set, my_lst)
print(symmetric_difference_set)

Output:

{2, 3, 4, 5}

The above example finds the symmetric difference of the set, my_set, and the list, my_list using the symmetric_difference() class method.

Symmetric Difference with Mutable Sets

Similar to other set operations, symmetric difference operations do not modify the original sets. Instead, they create a new set containing the symmetric difference. If you want to modify one of the original sets to contain the symmetric difference, you should do it explicitly by assigning the result back to it.

Example:

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.symmetric_difference(another_set) 
print(my_set)

Output:

{1, 2, 4, 5}

After this operation, the original set, my_set, contains the resulting symmetric difference, {1, 2, 4, 5}.

Summary & Reference for Reference to Symmetric Difference of Python Sets

Set symmetric difference is a fundamental set operation in Python for extracting elements that are in only one of the sets.


The symmetric_difference() instance method extracts elements unique to a set object and one other iterable, creating a new set.

my_set = {1, 2, 3}
my_lst = [1, 4, 5]
symmetric_difference_set = set.symmetric_difference(my_set, my_lst)  
# symmetric_difference_set --> {2, 3, 4, 5}

The symmetric difference operator, ^, allows you to find elements unique to only one set among two or more proper sets. It creates a new set containing only those unique values.

set1 = {1, 2, 3}
set2 = {1, 4, 5}
set3 = {2, 4, 6}
symmetric_difference_set = set1 ^ set2 ^ set3  
# symmetric_difference_set --> {3, 5, 6}

The symmetric_difference() class method of the set class lets you find the symmetric difference between a set object, given as the first argument, and another iterable object, given as the second argument.

my_set = {1, 2, 3}
my_lst = [1, 4, 5]
symmetric_difference_set = set.symmetric_difference(my_set, my_lst)   
# symmetric_difference_set --> {2, 3, 4, 5}

Set symmetric difference operations don’t modify the original sets but create new sets. If you want to modify one of the original set variables with the symmetric difference result, you should assign the result back explicitly.

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.symmetric_difference(another_set)  
# symmetric_difference_set --> {1, 2, 4, 5}