Python Symmetric Difference Examples [Tutorial]


Written By - Bashir Alam
Advertisement

Introduction to Python Symmetric Difference

The set which contains the elements which are either in set A or in set B but not in both is called the Python symmetric difference between two given sets. It is represented by A ⊝ B and is read as a symmetric difference of sets A and B. The symmetric difference in Python is the same. It contains a mix of items that are not present in both sets. The following diagram shows the symmetric difference between the two sets.

Python symmetric difference

As you can see, the figure shows the symmetric difference between two sets where the shaded region represents the symmetric difference between set A and set B. In other words, if there are set_A and set_B, then the symmetric difference between them will be equal to the union of set_A and set_B without the intersection between the two.

We will use the following two methods to find the symmetric difference in Python:

  • symmetric_difference() method
  • Using ^ operator

 

Method-1: Python Set symmetric_difference() Method

The symmetric_difference() method in Python is used to find the symmetric difference of the given sets. This method returns all the items present in given sets, except the items in their intersections.

Let us assume that A and B are two sets. Then the syntax for the symmertric_difference() method will be as follows.

A.symmetric_difference(B)

Here A and B are two sets.

 

Example-1: Python symmetric difference of two sets

Let us now find the symmetric difference of two sets using the symmteric_difference() method. First, we will create two sets in Python.

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}

As you can see, we have created two sets named A and B. Let us now apply the symmetric_difference() method.

Advertisement
# returns the symmetric difference 
result = A.symmetric_difference(B)

# printing the result
print(result)

Output:

symmetric-difference

As you can see, the symmetric difference method returned all the elements that were not in both of the sets.

One thing to note is that set A symmetric difference with set B is always equal to set B's symmetric difference with set A. For example, see the python code below:

# returns the symmetric difference 
result = B.symmetric_difference(A)

# printing the result
print(result)

Output:

symmetric-difference

As you can see, we get the same result.

Another important thing to consider is that when we take the symmetric difference of a set with itself, it returns an empty set as shown below:

# returns the symmetric difference 
result = B.symmetric_difference(B)

# printing the result
print(result)

Output:

emtpy-symteric

As you can see, we get an empty set because all the elements were common.

Advertisement

 

Example-2: Symmetric difference of multiple sets

Sometimes, we may want to find the symmetric difference of multiple sets. In such cases, when we have more than two sets, the symmetric difference methods fail to return the symmetric difference. For example, lets a say that we have four different sets and we want to find the symmetric difference among those.

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}
C = { 2,6,8,10,12,14,16}
D = {3,7,9,13,15}

# returns the symmetric difference 
result = A.symmetric_difference(B,C,D)

# printing the result
print(result)

Output:

error-in-symmteric-difference-method

As you can see, the error says that the symmetric difference method can only take one parameter at a time.

So in order to find the symmetric difference of multiple sets, we need to pass the result of two sets to the third one as shown below:

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}
C = { 2,6,8,10,12,14,16}
D = {3,7,9,13,15}

# returns the symmetric difference 
result = A.symmetric_difference(B.symmetric_difference(C.symmetric_difference(D)))

# printing the result
print(result)

Output:

multiple-symmetric-difference

As you can this, this time we were able to find the symmetric difference of multiple sets.

Advertisement

 

Method-2: Using ^ Operator

Example-3: Using ^ operator

The ^ operator in Python is also known as the Bitwise XOR operator. The bitwise XOR operator can also be used to find the symmetric difference of sets. Let us first apply the bitwise XOR operator on two sets to find the symmetric difference.

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}

# works as (A).symmetric_difference(B)
print(A ^ B)

Output:

bitwise-operator

As you can see, the bitwise XOR operator has returned the symmetric difference of the given sets.

Similarly to the symmetric difference method, A^B is equal to B^A as shown below:

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}

# works as (A).symmetric_difference(B)
print(B ^ A)

Output:

bitwise-operator

As you can see, we get the same result.

 

Example-4: Multiple sets using ^ operator

Finding symmetric differences of multiple sets using a bitwise XOR operator is very simple. Let us first create multiple sets and then find the symmetric difference.

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}
C = { 2,6,8,10,12,14,16}
D = {3,7,9,13,15}

# works as (A).symmetric_difference(B)
print(A ^ B ^ C ^ D)

Output:

multiple-symmetric-difference

The order of sets while finding symmetric differences does not matter. For example, see the code below:

# creating sets in Python
A = {1,2,3,4,5,6,7,8}
B = {4,5,6,7,8,9,0,10,11}
C = { 2,6,8,10,12,14,16}
D = {3,7,9,13,15}

# works as (A).symmetric_difference(B)
print(C ^ A ^ B ^ D)

Output:

Advertisement

multiple-symmetric-difference

As you can see, we get the same result.

 

Summary

The symmetric difference between two sets is the set of elements that are in either of the sets but not in both of them. symmetric_difference() method in Python returns a new set that contains the symmetric difference of two sets. Also, we can use the bitwise XOR operator to find the symmetric differences. In this short article, we discuss a couple of methods through which we can find the symmetric difference in Python using various examples.

 

Further Reading

Python sets
Symmetric difference

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment