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 open()
Python compile()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String strip()
Python Program to Remove Duplicate Element From a List
Convert a Map to an Array, List or Set in Java
Python filter()
Python List index()
Python print()
Python Set remove()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python __import__()
Python Matrices and NumPy Arrays
Python String isspace()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Trim Whitespace From a String
Python setattr()
Python Program to Add Two Numbers
Python time Module
Python String rjust()
Python Strings
Python Program to Get Line Count of a File
Python Closures
Deep Learning in Python - LazyProgrammer
Python abs()
Python String format()
Python String find()
Python Program to Find the Factorial of a Number
Python Program to Concatenate Two Lists
Python Program to Differentiate Between type() and isinstance()
Python Program to Extract Extension From the File Name
Python Numbers, Type Conversion and Mathematics