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:
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python List pop()
Python Program to Convert Decimal to Binary Using Recursion
Python ord()
Python Directory and Files Management
Python staticmethod()
Python Program to Find Numbers Divisible by Another Number
Python Program to Get the Full Path of the Current Working Directory
Python Program to Create Pyramid Patterns
Python Program to Find Factorial of Number Using Recursion
Python Object Oriented Programming
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Solve Quadratic Equation
Python Program to Make a Flattened List from Nested List
Python dir()
Python Program to Compute all the Permutation of the String
Python Program to Convert Bytes to a String
Python Data Types
Python hasattr()
Python Set update()
Python String title()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Split a List Into Evenly Sized Chunks
Python String split()
Python Function Arguments
Python List append()
Python Program to Find the Largest Among Three Numbers
Python String isspace()
JavaScript Map and Set
Python Program to Concatenate Two Lists
Python bin()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...