Table of Contents
The Python symmetric_difference() method returns the symmetric difference of two sets.
The symmetric difference of two sets A and B is the set of elements that are in either A or B, but not in their intersection.

The syntax of symmetric_difference()
is:
A.symmetric_difference(B)
1. Example 1: Working of symmetric_difference()
A = {'a', 'b', 'c', 'd'} B = {'c', 'd', 'e' } C = {} print(A.symmetric_difference(B)) print(B.symmetric_difference(A)) print(A.symmetric_difference(C)) print(B.symmetric_difference(C))
Output
{'b', 'a', 'e'} {'b', 'e', 'a'} {'b', 'd', 'c', 'a'} {'d', 'e', 'c'}
2. Symmetric difference using ^ operator
In Python, we can also find the symmetric difference using the ^
operator.
A = {'a', 'b', 'c', 'd'} B = {'c', 'd', 'e' } print(A ^ B) print(B ^ A) print(A ^ A) print(B ^ B)
Output
{'e', 'a', 'b'} {'e', 'a', 'b'} set() set()
Related posts:
Python Program to Convert String to Datetime
Python dir()
Python frozenset()
Python list()
Python Program to Safely Create a Nested Directory
Python float()
Python Global, Local and Nonlocal variables
Python Set discard()
Python staticmethod()
Python Set remove()
Python Set symmetric_difference_update()
Python range()
Python Program to Represent enum
Python Set issubset()
Python Tuple
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Find Numbers Divisible by Another Number
Python List insert()
Python Program to Catch Multiple Exceptions in One Line
Python String capitalize()
Python String isalpha()
Python reversed()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String encode()
Python Variables, Constants and Literals
Python tuple()
Python Program to Randomly Select an Element From the List
Python Dictionary update()
Python Dictionary pop()
Deep Learning in Python - LazyProgrammer
Python String lower()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho