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 min()
Python String splitlines()
Python Program to Count the Number of Each Vowel
Python sorted()
Python Program to Find Numbers Divisible by Another Number
Python delattr()
Python Program to Generate a Random Number
Python String maketrans()
Python Program to Get the File Name From the File Path
Python Program to Get a Substring of a String
Python round()
Python Program Read a File Line by Line Into a List
Python locals()
Python Program to Add Two Numbers
Python Program to Count the Occurrence of an Item in a List
Python ascii()
Python String lstrip()
Python List clear()
Python super()
Python Dictionary copy()
Python Program to Remove Punctuations From a String
Python String rindex()
Python Program to Check If a List is Empty
Python List append()
Python List reverse()
Python Program to Check Armstrong Number
Python Get Current time
Python __import__()
Python Program to Get Line Count of a File
Python String isalpha()
Python strftime()
Python String format_map()