## 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.

`C=A.symmetric_difference(B)`

### 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
A={18,13,48,36,65}
B={22,36,54,13,5}
# Performing symmetric_difference operation and storing result in new set C
C=A.symmetric_difference(B)
#Printing a result
print("A ^ B is ",C)
```

Output

```
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
r1=sport.symmetric_difference(computer)
#Printing a set
print("Students who selected computer or sport but not both ",r1)
```

Output

```
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
s1={10,20,30}
#Initializing the list, tuple and dictionary
l1=[12,20,22]
t1=(30,65,85)
d1={"Name":"John","Salary":50000,"Experience":10}
#Using update method and printing after each update
r1=s1.symmetric_difference(l1)
print("symmetric_difference with set and list",r1)
r2=s1.symmetric_difference(t1)
print("symmetric_difference with set and tuple",r2)
r3=s1.symmetric_difference(d1)
print("symmetric_difference with set and dictionary",r3)
```

Output

```
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}
```

### Conclusion

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

### References

Happy Learning ðŸ™‚