Python Set Symmetric Difference Method
In this Python tutorial, we ‘ll learn about Python set symmetric difference method in detail. We’ll demonstrate its role and proper use with the help of practical Python code examples.
Introduction: Python Set Symmetric Difference Method
This method returns a set which contains items that are unique in all specified sets. Let’s now understand this concept with the help of multiple Python examples. But first, let’s walk ourselves through its syntax.
Syntax of symmetric_difference() Method
setA.symmetric_difference(setB)- This method takes a single argument. This argument can be a set, list, tuple, dictionary or a string.
- It returns a set that contains unique items of the 2 sets.
- Passing no argument to this method will result in raising a type error exception.
Get Zeeshan Ali’s stories in your inbox
Join Medium for free to get updates from this writer.
Implementing Python Set Symmetric Difference Method (Easy Examples)
Let’s try to understand the proper use of symmetric_difference() method with the help of following examples.
Example 1: symmetric_method() applied on 2 Python Sets
sA={4,3,2}
sB={5,4,2}
print( sA.symmetric_difference(sB) )Output
{3, 5}- First, we’ve created 2 simple sets containing integer items.
- Then we’ve called the symmetric_difference() method of first set(sA) and have passed second set(sB) as an argument to it.
- We’ve used this method inside print function to display the result. We can see that a set has been returned which contains only unique items of both sets. Continue reading…




