The symmetric method_difference

The symmetric_difference method eliminates common elements for a set and a sequence, while returning a new set containing only the different elements. In the parameter, we specify the set with which we want to compare the original.

Syntax

pythonset.symmetric_difference(the set with which we want to compare)

Example

Let's apply the symmetric_difference method to get the distinct elements for two sets:

pythonst1 = {'a', 'b', 'c'}
st2 = {'e', 'g', 'a'}
res = st1.symmetric_difference(st2)
print(res)

Result of code execution:

python{'g', 'b', 'c', 'e'}

Example

The symmetric_difference method also has a short form:

pythonst1 = {'a', 'b', 'c'}
st2 = {'e', 'g', 'a'}
res = st1 ^ st2
print(res)

Result of code execution:

python{'g', 'c', 'b', 'e'}

See also

  • method difference,
    which returns the differences between sets
  • method issubset,
    which checks the presence of elements of a set in a sequence
  • method issuperset,
    which checks the presence of elements of a sequence in a set
  • method intersection,
    which returns the intersection of sets