How symmetric_difference_update() works in Python

Python has a plethora of in-built methods to make carrying out simple tasks much easier. One such method is symmetric_difference_update().

This is used to find the symmetric difference of two sets and it updates the set that calls the method. The elements of that set are passed as an argument.

Figure 1: Symmetrical Difference between two sets X and Y, will be the elements present in both X and Y but not in their intersection.

Syntax

set1.symmetric_differences_update(set2)

Parameters and return value

This method requires one set to be passed as a parameter, which it will compare with the set that calls the method.

It is important to note that this returns None and only updates the set calling it.

Example

x = {'Cats', 'Dogs', 'Elephants', 'Seals'}
y = {'Dogs', 'Lions', 'Seals', 'Parrots'}
x.symmetric_difference_update(y)
print(x)

Free Resources