## Python symmetric*difference*update explanation with an example :

*symmetric*difference*update* method is used to find the symmetric difference of two sets in python.

For two sets *setA* and *setB*, the symmetric difference is the set of all elements that are either in *setA* or *setB* but not in both.

For example :

```
setA = {1,2,3,4}
setB = {3,4,5,6}
```

The symmetric difference between above two sets is *{1,2,5,6}*.
Only *1,2,5,6* are either in the first set or in the second set.*3 and 4* are in both *setA* and *setB*.

### Definition :

We can define *symmetric*difference*update* method as below :

`setA. symmetric_difference_update(setB)`

As you can see that this method takes one *Set* as the argument. It will return *None*. It updates the set that is calling, i.e. *setA* with the result. Or after the method is called, *setA* will hold the symmetric difference of both sets.

### Example Program :

```
#1
setA = set()
setB = set()
#2
setA_length = int(input(“Enter the size of the first set : “))
setB_length = int(input(“Enter the size of the second set : “))
#3
print(“\n”)
print(“Enter values for the first set one by one : \n”)
for i in range(setA_length):
e = int(input(“Enter value {} : “.format(i+1)))
setA.add(e)
#4
print(“\n”)
print(“Enter values for the second set one by one : \n”)
for i in range(setB_length):
e = int(input(“Enter value {} : “.format(i+1)))
setB.add(e)
#5
print(“\nBefore calling symmetric_difference_update : “)
print(“SetA : {} “.format(setA))
print(“SetB : {} “.format(setB))
#6
setA.symmetric_difference_update(setB)
#7
print(“\nAfter symmetric_difference_update is called : “)
print(“SetA : {} “.format(setA))
print(“SetB : {} “.format(setB))
```

### Explanation :

*The commented numbers in the above program denote the step numbers below :*

- Create two empty sets setA and setB.
- Get the length of these sets from the user and put them in setA
*length and setB*length. - Read the elements of the first set and add them to setA.
- Similarly, read the elements of the second set and add them to setB.
- Print the sets to the user.
- Call symmetric
*difference*update to setA and setB using setB as the argument. - Print the sets again with the new values in setA.

### Sample Output :

```
Enter the size of the first set : 4
Enter the size of the second set : 4
Enter values for the first set one by one :
Enter value 1 : 1
Enter value 2 : 2
Enter value 3 : 3
Enter value 4 : 4
Enter values for the second set one by one :
Enter value 1 : 3
Enter value 2 : 4
Enter value 3 : 5
Enter value 4 : 6
Before calling symmetric_difference_update :
SetA : {1, 2, 3, 4}
SetB : {3, 4, 5, 6}
After symmetric_difference_update is called :
SetA : {1, 2, 5, 6}
SetB : {3, 4, 5, 6}
```

### Conclusion :

We have learned how to use *symmetric*difference*update* in python. This method is useful if we need to filter out all common elements from two sets. Try to run the example shown above and drop one comment below if you have any queries.

### Similar tutorials :

- Python set isdisjoint() explanation with an example
- Python set discard method explanation with an example
- Python gamma() function explanation with example
- Python string ljust() method explanation with example
- Python Set difference_update explanation with an example
- Python Set pop() method explanation with example