Python Set symmetric_difference_update
Now, we are going to learn about the symmetric_difference_update method. Even before that, Let’s have a quick revision of what we mean by symmetric difference operation. Let’s say that we have two sets, set1, and set2.
Now, if we do set1.symmetric_difference(set2), we get a new set, which is a symmetric difference between the two sets. By this, we simply mean that the new set is going to contain the elements that are in the given sets, but not in their intersection. But remember that with the symmetric_difference method, we get the new set.
With the symmetric_difference_update method, we do not get a new set. Instead, the changes will be made on the set, that calls the method. For example, if we are doing set1.symmetric_difference_update(set2), the changes are made to set1. So, this method returns None.
Python Set symmetric_difference_update() Method
Let’s have a look at a simple example, which demonstrates the symmetric_difference_update method.
As you can see in the above program, we have two sets, set1, and set2. Then we are trying to call the method symmetric_difference_update method on the set1, so the changes would be made to the set1. Also, in the last instruction, we are printing set1. So, the output in our case is going to be –
{‘W’, ‘Z’, ‘J’}
As you can see, we got the changes into set1. So, as and when required, we can make use of the method symmetric_difference_update in our programs. Remember that the method does not return a new set. Instead, the changes are done to the set on which, the method was called