| by Arround The Web | No comments

Python Set Symmetric_Difference() Method

The set that contains all elements that are either in a particular set but not in the other or both the given sets but not in common is known as the symmetric difference in mathematics. It can be considered as a union of either two or more sets excluding the elements that are common in the given sets. It’s a technique to identify what makes each set distinct. Set is a library that is offered by Python for algorithms and data structures. Although the type “set” has many intriguing features, working with symmetric differences of sets is made simple by a built-in function in Python. In this article, we will examine the symmetric difference and its use in Python.

What Is the Symmetric_Difference() Function in Python?

A symmetric difference set of the two specified sets is returned by the set symmetric_difference() method. The elements that are stored in either set 1 or set 2 but not in both are contained in another set which is a symmetric difference set of the two given sets, i.e. sets 1 and 2. As an example, the symmetric difference between the sets {1, 2, 3} and {3, 2, 4} would be {1, 4} due to the presence of elements 2 and 3 in both sets.

Syntax:

set.symmetric_difference(other_set)

 
Parameters:

other_set: The set that is used to determine the symmetric difference.

How Can We Find the Symmetric Difference Set in Python?

The symmetric difference set can be obtained using two methods in Python. The first method is using the symmetric_difference() function and the second method is using the “^” operator. However, our main focus is finding the symmetric difference set using Python’s “symmetric_difference()” in-built function.

Method 1: Find the Symmetric Difference Using the Symmetric_Difference() Function

This function allows only one parameter to be passed as an argument. The iterable types such as set, tuple, list, dictionary, and string are all acceptable inputs for this function.

Example 1: Finding the Symmetric Difference Between Two Integer Sets

First, we need two sets from which we find the symmetric difference set. The sets can be created simply by putting the data elements inside the curly braces “{}” separated by commas.


As can be seen, our two integer sets “A” and “B” are created which store the data values (3, 5, 6, 1, 4, 9, 11) and (2, 4, 3, 5, 6, 14, 9), respectively. Now, we apply the symmetric_difference() function to find the symmetric difference set from the sets “A” and “B”.


We applied the function on set “B” and the set “A” is passed as an input parameter inside the function. The function returned a symmetric difference set with data values (1, 2, 11, 14). All the common elements between both sets 2, 3, 4, 5, 6, and 9 are excluded by the function.

Example 2: Finding the Symmetric Difference Between Two String Sets

This time, we create the sets which contain the string values. Then, we find the symmetric difference between them.


Our required sets X, Y, Z are created with the string values (“a”, “d”, “g”, “h”, “j”, “k”, “l”, “c”), (“v”, “a”, “d”, “g”, “h”, “d”, “j”), and (“s”, “f”, “a”, “k”, “V”, “j”, “h”), respectively. Using the symmetric_difference() function, we cannot find the symmetric difference of more than two sets. So, we find the symmetric difference between sets X and Y first.


The symmetric set with elements (‘v’, ‘l’, ‘k’, ‘c’) is returned by the function. Now, let’s check for the sets “Y” and “Z”.


The function excludes all the common entries between both sets and returns a symmetric difference set with values (‘d’, ‘s’, ‘v’, ‘g’).

Example # 3: Finding the Symmetric Difference Between a Set and a List Object

We have seen how the symmetric difference between two sets can be obtained. Now, we find the symmetric difference between a set and a list object using the symmetric_difference() method.


We created the required list “L” and set “S” with the items/values (1, 2, 3, 4, 5, 6, 7) and (3, 4, 7, 1, 5, 8, 9), respectively. Now, let’s use the symmetric_difference() function to obtain the symmetric difference.


We applied the function on set “S” and the list “L” is passed as an argument for the function. By comparing the elements of set “S” with the items of list “L”, the function returned an output set with values (2, 6, 8, 9).

Example # 4: Finding the Symmetric Difference Between a Set and a Tuple in Python

Now, we need a tuple and a set. Just like a list, multiple items can be stored in a specific variable using tuples. To create a simple Python tuple, we can just add/store the items inside the round braces “()” separated by commas.


A tuple “T” is created with the items (2, 1, “a”, 5, 3, “b”, 4). The elements (“a”, 4, “c”, 1, 3, 6, 5, 2) are stored in the set “S”. We can now find the symmetric difference set.


We applied the function on set “S” and the tuple “T” is passed as an argument inside the function. The symmetric difference with elements {‘b’, 6, ‘c’} is obtained by the function.

Example 5: Finding the Symmetric Difference Between a Set and a Dictionary in Python

Now, in this example, we will check the symmetric difference between a set and a Python dict. Let’s first create our required dictionary and set.


Our dictionary is created with the key:value pairs (‘a’: 1, ‘s’: 5, ‘d’ : 10, ‘f’: 15, ‘g’: 20, ‘h’: 25). We also created the set “S” with data entries (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’). Now, let’s obtain the symmetric difference.


By comparing the data stored dictionary “D” and set “S”, the function returned the  symmetric difference set { ‘h’, ‘b’, ‘e’, ‘s’, ‘g’, ‘c’}.

Method 2: Find the Symmetric Difference Using the “^” Operator

The symmetric difference between two sets or between the sets and iterables can also be obtained using the “^” operator.

Example:

We have seen how a symmetrical difference set can be found using the symmetric_difference() method. Now, in this example, the “^” operator is used to do so. Let’s create our sets first.


We created the sets s1 and s2 which contain the data values (2, 4, 6, 8, 10) and (1, 2, 3, 4, 5), respectively.  Now, let’s use the “^” operator.


The operator returned the symmetric difference of sets s1 and s2. With the use of the “^” operator, we can also find the symmetric difference set of multiple sets which is not possible using the symmetrical_difference() function.


As you can observe, the operator has successfully obtained the correct result/output.

Conclusion

In this tutorial, we tried to teach you what symmetrical difference is and how we can find it in Python. First, we explained the working and syntax of the symmetrical_difference() function. Then, we demonstrated the multiple examples to show you how to find the symmetric difference between two sets, between a list and a set,  between a tuple and a set, and between a dictionary and a set. We also implemented an example using the “^” operator to find the symmetrical difference between two and multiple sets.

Share Button

Source: linuxhint.com

Leave a Reply