Python Symmetric Difference

Summary: in this tutorial, you’ll learn how to find the symmetric difference between two or more sets in Python.

Introduction to the symmetric difference of sets #

The symmetric difference between two sets is a set of elements that are in either set, but not in their intersection.

Suppose that you have the following s1 and s2 sets:

s1 = {'Python', 'Java', 'C++'}
s2 = {'C#', 'Java', 'C++'}Code language: JavaScript (javascript)

The symmetric difference of the s1 and s2 sets returns in the following set:

{'C#', 'Python'}Code language: JavaScript (javascript)

As you can see clearly from the output, the elements in the return set are either in s1 or s2 set, but not in their intersection.

The following Venn diagram illustrates the symmetric difference of the s1 and s2 sets: