In Python, the symmetric_difference() method is a built-in function of the set data type that returns the set of elements that are in one set or the other, but not in both sets.
Here's an example of how to use the symmetric_difference() method:
set1 = {1, 2, 3, 4, 5}set2 = {3, 4, 5, 6, 7}sym_diff_set = set1.symmetric_difference(set2)print(sym_diff_set) |
Output:
{1, 2, 6, 7} |
In this example, we define two sets, set1 and set2, that have some overlapping elements. We use the symmetric_difference() method on set1 and pass in set2 as an argument to return the set of elements that are in set1 or set2, but not in both sets. The resulting set, sym_diff_set, contains the elements 1, 2, 6, and 7.
Note that the symmetric_difference() method does not modify the original set. Instead, it returns a new set that contains the result of the symmetric difference operation.
Also note that the order of the arguments passed to symmetric_difference() does not matter. The resulting set will contain the elements that are in either set but not in both sets.