Python Set Symmetric_Difference():

The symmetric_difference() method creates a new set containing elements that are not common in both sets. In other words, this method works in the opposite way to the intersection method.

Method signature

The signature for the symmetric_difference() method is as shown below. Here, It selects all the elements from A and B that are not commmon in both, and the result is stored in set C.


Method parameters and return type

  • This method takes exactly one parameter. Furthermore, If the parameter passed is a list, tuple or dictionary then it automatically converts it into a set and performs the task.
  • However, this method returns a new set, consisting of only those values that are not common in both sets.

Python Set Symmetric_Difference Examples:

Example 1:

In this example, We will take a set A and set B with a set of numbers.

#Initializing the Set
# Performing symmetric_difference operation and storing result in new set C
#Printing a result
print("A ^ B is ",C)


A ^ B is  {48, 65, 18, 5, 54, 22}

Example 2:

In this case, let us consider a set of students opting for various optional subjects. At this point, If we want to find the list of students who selected either computer or sport but not both. So, we will use this method to get desired result.

#Initializing the Set
computer={"Ram", "Jimmy", "Keya"}
sport={"Sam","Keya", "Tom"}
#Using symmetric_difference method
#Printing a set
print("Students who selected computer or sport but not both ",r1)


Students who selected computer or sport but not both  {'Sam', 'Tom', 'Jimmy', 'Ram'}

Example 3:

In this example, let us demonstrate this method on a list, tuple and dictionary. For this example, we are storing the results in separate set and printing the same.

#Initializing the Set
#Initializing the list, tuple and dictionary
#Using update method and printing after each update
print("symmetric_difference with set and list",r1)
print("symmetric_difference with set and tuple",r2)
print("symmetric_difference with set and dictionary",r3)


symmetric_difference with set and list {10, 12, 22, 30}
symmetric_difference with set and tuple {65, 10, 20, 85}
symmetric_difference with set and dictionary {'Salary', 10, 'Name', 'Experience', 20, 30}


The symmetric_difference() method returns a set containing elements that are not common in both the given sets.


Happy Learning 🙂

Leave A Comment