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 Extract Extension From the File Name
Python Numbers, Type Conversion and Mathematics
Python bool()
Python callable()
Python Package
Python String encode()
Machine Learning with Python for everyone - Mark E.Fenner
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Set difference_update()
Python Program to Check Leap Year
Python Input, Output and Import
Python help()
Python Program to Get Line Count of a File
Deep Learning with Python - Francois Cholletf
Python Set issubset()
Python bytearray()
Python Program to Convert Two Lists Into a Dictionary
Deep Learning in Python - LazyProgrammer
Python Program to Get a Substring of a String
Python del Statement
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String zfill()
Python Anonymous / Lambda Function
Python sorted()
Python abs()
Python Set symmetric_difference_update()
Python Custom Exceptions
Python String translate()
Python List extend()
Python Decorators
Python sum()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty