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 List copy()
Python Recursion
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Function Arguments
How to get current date and time in Python?
Python Errors and Built-in Exceptions
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Convert Celsius To Fahrenheit
Python frozenset()
Python break and continue
Python setattr()
Python Program to Get Line Count of a File
Python Program to Catch Multiple Exceptions in One Line
Python float()
Python time Module
Python Program to Iterate Through Two Lists in Parallel
Python Program to Swap Two Variables
Python String isdigit()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set pop()
Python File I/O Operation
Python Program to Count the Occurrence of an Item in a List
Python tuple()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Variables, Constants and Literals
Python sleep()
Python Program Read a File Line by Line Into a List
Python Program to Find Hash of File
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Converting Between an Array and a Set in Java
Python String rstrip()
Python len()