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.
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.
Example 2: symmetric_method() applied on Iterables
setA = {1,6,7,8,9,10} print( setA.symmetric_difference([7,8,4,3,2]) ) print( setA.symmetric_difference(('a',4,2,5)) ) print( setA.symmetric_difference({'a':54,'b':9}) ) print( setA.symmetric_difference('set9') )
Output
{1, 2, 3, 4, 6, 9, 10} {1, 2, 4, 5, 6, 7, 8, 9, 10, 'a'} {1, 6, 7, 8, 9, 10, 'a', 'b'} {1, 'e', 6, 't', 7, 8, 9, 10, 's', '9'}
- First we’ve created a Python set.
- Then we’ve called symmetric difference method of this set and have passed iterable as an argument to it. These are list, tuple, dictionary and string.
- As a result, new sets were returned which contains only unique items.
Example 3: No Argument Passed To Symmetric Difference Method
stA={'a','b','c','d'} print( stA.symmetric_difference() )
Output
TypeError: symmetric_difference() takes exactly one argument (0 given)
In this program, we haven’t passed any argument to this method. We can see that a type error exception has been raised which shows that one argument is required for this method.
Example 4: When No Unique Items Are Found
a = {1,2,3} b = {3,1,2} print( a.symmetric_difference(b) )
Output
set()
We can see that set() is returned by this method when no unique item exists in the specified sets. The set() denotes an empty set.
Example 5: Return Value Of Python Set Symmetric Difference Method
a = {'e','f','g','h'} b = {'r','k','e','h'} returnedValue = a.symmetric_difference(b) print('Return value of this method is: ', returnedValue)
Output
Return value of this method is: {'g', 'k', 'r', 'f'}
- For demonstration, we’ve created 2 Python sets.
- Then we’ve applied symmetric difference method on them and have stored the return value inside a variable.
- In order to see the result, we’ve displayed value of this variable using Python print function.
- In shows a set of unique items of both these sets. It means this method returns a set which contains only unique items of all sets.
Example 6: Python For Loop to Apply Symmetric Difference on Python Lists of Sets
a=[{'a','b'},{4,6,7},{23,'abc'}] b=[{'r','b','e'},{45,6,21},{23,'abc'}] nLst=[] for val1,val2 in zip(a,b): nLst.append(val1.symmetric_difference(val2)) print(nLst)
Output
[{'e', 'a', 'r'}, {4, 7, 45, 21}, set()]
- In this code example, we’ve first created 2 Python list of sets. Also, an empty list has been specified which will store the resulting sets.
- After that, we’ve specified a Python for loop which will take all the items of these two lists(these items are Python sets). Click here to learn more about iterating over multiple Python lists simultaneously.
- Inside the body of this loop, symmetric difference method will be applied on all the sets of both lists and the result will be added to the new list using its append method.
- Finally, we’ve printed the new list in order to see the result and it shows a list of sets. It can be seen in the above output section.
Example 7: Using ^ Operator To Find Symmetric Difference
a={1,2,3,5,7} b={2,5,10,7,3,10} c={22,5,123,7,100,10} print(a^b) print(a^b^c)
Output
{1, 10} {1, 100, 5, 22, 7, 123}
By using the (^) operator, we can return a set of unique items between two sets. In case of three sets, it’ll pick only the items that are present in one set and also the items that are present in all three sets. Try it with more than 3 sets and share your experience with us.
So this is how we can easily and properly make use of Python set symmetric difference method. We’d be delighted to see you share this post among other Python developers.
Click here to learn more about Python set methods.
Do feel free to ask if you still have any questions related to Python set symmetric difference method. We’ll be super glad to answer all.
Conclusion
In conclusion of this tutorial, now we’ve an in depth knowledge of how to properly use Python set symmetric difference method. Please do feel free to leave your informative feedback on this post.
Other valuable articles on Python programming are available on this site, so don’t hesitate to visit them as well. Thank you for reading this article.