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 Generate a Random Number
Python Dictionary get()
Python String zfill()
Python Dictionary keys()
Python Set intersection()
Python Tuple
Python Input, Output and Import
Python Closures
Python Program to Find the Factors of a Number
Python String lower()
Python Program to Differentiate Between type() and isinstance()
Python Program to Add Two Matrices
Python hash()
Python List copy()
Python String strip()
Convert a Map to an Array, List or Set in Java
Python any()
Python Program to Sort a Dictionary by Value
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Solve Quadratic Equation
Intelligent Projects Using Python - Santanu Pattanayak
Python @property decorator
Python zip()
Python String maketrans()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String replace()
Python all()
Python Set pop()
Python Set discard()
Python Program to Slice Lists
Python String isdecimal()
Python Program to Append to a File