Python Set - symmetric_difference method

The symmetric_difference method returns a set with elements from both the lists but excludes the elements that are common to both the sets

Set Updated 16 Sep 2016 1 min read

Method Signature

symmetric_difference(anotherSetObject_in)

Overview

A difference set is returned - with elements that are present in either of the sets.
However the elements that are present in both the sets will be excluded.

Parameters

anotherSetObject_in: The set to be compared to produce the difference set.

Example

python
# Example Python program that finds the symmetric difference

            # between two sets

            sportList1    = {"Football", "Golf", "Hockey", "Polo", "Tennis"}

            sportList2    = {"Carrom", "Table tennis", "Chess", "Tennis"}

# Find the symmetric difference

            symDiff     = sportList1.symmetric_difference(sportList2)

            print(symDiff)

Output

Output
{'Carrom', 'Table tennis', 'Golf', 'Football', 'Chess', 'Hockey', 'Polo'}