In this Python tutorial, we’ll learn about Python set symmetric difference update method. We’ll uncover its purpose and proper use via practical code examples.
Introduction: Python Set Symmetric Difference Update
It’s a method of Python set and its used to find the unique items in all sets and updates its calling set with those items. It means if we are using the symmetric difference update method of set A then only the unique items of all sets will be stored in that set after the executing of this method.
Let’s first walk through the syntax of this method. After that, we’ll jump into multiple Python code examples in order to understand the implementation of this method in detail.
Syntax
setX.symmetric_difference_update(setY)
- Python set symmetric difference update method takes only one argument. It can be a Python tuple, set, dictionary, list or a Python string.
- Passing no argument to this method will raise a type error exception. It means one argument is required for this method.
- Return value of this method is None. It does not return anything, it just updates its calling method(setX in this case) with the resulting unique items.
- In case, no unique items are found then this method updates its calling set with set() which denotes an empty set.
Implementing Python Set Symmetric Difference Update Method (Examples)
Let’s learn the proper use of this method with the help of following examples.
Example 1: Applied on Sets
a = {1,2,3} b = {1,6,5} a.symmetric_difference_update(b) print('Set A is: ',a) print('Set B is: ',b)
Output
Set A is: {2, 3, 5, 6} Set B is: {1, 5, 6}
- In this example code, we’ve created 2 Python sets.
- After the creating of sets, we’ve called the symmetric difference update method of first set(a) and have passed the second set(b) as an argument to it.
- To see the results, we’ve printed both these sets and we can see that the calling set(a) has been updated with the unique items of both sets while the second set(b) remains the same.
Example 2: Passing Iterables to this Method
x={11,22,33,44} x.symmetric_difference_update([22,88,5,6,'y']) print(x) x.symmetric_difference_update((1,2,3)) print(x) x.symmetric_difference_update({'a':5,'b':10}) print(x) x.symmetric_difference_update('Python') print(x)
Output
{33, 'y', 5, 6, 11, 44, 88} {33, 1, 2, 3, 'y', 5, 6, 11, 44, 88} {33, 1, 2, 3, 'y', 5, 6, 11, 44, 'a', 88, 'b'} {33, 1, 2, 3, 5, 6, 't', 'n', 'P', 11, 44, 'a', 'h', 'o', 88, 'b'}
- In this program, we’ve passed iterables as an argument to symmetric difference update method.
- First we’ve created a set. Then we’ve called its symmetric_difference_update() method and have passed Python list as any argument to it. After that, we’ve printed the calling set(x).
- Using the same process, we’ve passed tuple, dictionary and string as an argument to this method.
- The result can be seen in the above output section.
Example 3: No Argument Passed
j = {8,7,9,10} print(j.symmetric_difference_update())
Output
TypeError: symmetric_difference_update() takes exactly one argument (0 given)
As you can see that passing no argument to this method will result in a type error exception. It means one argument(it can be a tuple, set, list, dictionary or string) is compulsory for the symmetric difference update method to work without any errors.
Example 4: No Unique Items Found
aa={7,8,9} bb={8,7,9} aa.symmetric_difference_update(bb) print(aa)
Output
set()
As you can see that this method updates its calling set with set() when no unique items are found. The set() denotes an empty Python set.
Example 5: Return Value of Python Set Symmetric Difference Update Method
j = {'Zeeshan','Yameen','Usman','Zain'} k = {'Zain','Hasan','Saqib'} valueReturned = j.symmetric_difference_update(k) print( valueReturned )
Output
None
- For the sake of demonstration, we’ve first created 2 sets.
- Then we’ve used the symmetric difference update method of first set(j) and have specified second set(k) as its argument.
- After that, we’ve stored the value returned by this method inside a variable.
- Finally, we’ve printed the value of variable and it shows None.
- It means this method does not return anything. It just updates its calling method with the resulting unique items of both sets.
Example 6: Method Applied on Python List of Sets Using For Loop
val1=[{1,2},{'a','v'},{2.4}] val2=[{4,2},{'aa','v'},{7.8,5,6}] newSetsList=[] for v1,v2 in zip(val1,val2): v1.symmetric_difference_update(v2) newSetsList.append(v1) for value in newSetsList: print(value)
Output
{1, 4} {'a', 'aa'} {2.4, 5, 6, 7.8}
- In this block of code, first we’ve specified 2 list of Python sets.
- Also, an empty Python list has been created for the purpose of storing the updated list of sets.
- Then, we’ve specified a for loop of Python which will store each value of both lists inside the specified variables while iterating over them. Click here to learn more about iterating over multiple Python lists simultaneously.
- In the body of for loop, we have first called the symmetric difference update method using items of first list(val1) and have passed items of second list(val2) as any argument to it.
- After that, we have added the updated sets to new list using its append method.
- To show the results, we’ve used another for loop just to show items of new list. We can see it using a simple print function as well.
- The result can be seen in the above output section. Actually, we didn’t have to use a new list to see the updated items. We can see the updated items by displaying the list whose items(sets) were used to call the symmetric difference update method.
So this is how we can properly use Python set symmetric difference update method. Don’t hesitate to 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 queries related to Python set symmetric difference update method. We’ll be super glad to solve all.
Conclusion
In conclusion of this tutorial, this is how we can properly use Python set symmetric difference update method. Please don’t hesitate to leave your valuable feedback in the comment section.
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.