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:
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
JavaScript Map and Set
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Get Line Count of a File
Python for Loop
Python Program to Find the Factors of a Number
Python String isupper()
Python Set clear()
Python Shallow Copy and Deep Copy
Python enumerate()
Python List pop()
Python Set difference_update()
Python del Statement
Python pass statement
Python callable()
Python Program to Multiply Two Matrices
Python Dictionary keys()
Python Object Oriented Programming
Python sorted()
Python String startswith()
Intelligent Projects Using Python - Santanu Pattanayak
Python Objects and Classes
Python Directory and Files Management
Python String count()
Python Input, Output and Import
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python __import__()
Python Program to Print Output Without a Newline
Python input()
Python Program to Append to a File
Python filter()
Python Program to Create a Long Multiline String