Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3
data structures
communitycreator

How symmetric_difference_update() works in Python

Hassan Ahmed

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)

RELATED TAGS

python
python3
data structures
communitycreator
RELATED COURSES

View all Courses

Keep Exploring