Python Set symmetric_difference() Method – Tutorial with Examples

A set is an unordered collection of unique elements in Python. One of the built-in methods provided by Python for manipulating sets is the `symmetric_difference()` method. The `symmetric_difference()` method returns a new set that contains elements that are in either of the sets but not in both.

Table of Contents

Syntax

```set1.symmetric_difference(set2)
```

The `symmetric_difference()` method takes one argument, which is the set to compare with. It can also take multiple sets as arguments.

Return Value

The `symmetric_difference()` method returns a new set that contains elements that are in either of the sets but not in both.

Examples

Example 1: Using symmetric_difference() method to find the symmetric difference between two sets

Let’s create two sets and use the `symmetric_difference()` method to find the symmetric difference between them:

```# Create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Find the symmetric difference between the two sets
result_set = set1.symmetric_difference(set2)

# Print the result set
print(result_set)
```

Output:

`{1, 2, 3, 6, 7, 8}`

In the above example, we created two sets (`set1` and `set2`) and used the `symmetric_difference()` method to find the symmetric difference between them. The resulting set (`result_set`) contains the elements that are in either `set1` or `set2`, but not in both.

Example 2: Using symmetric_difference() method with multiple sets

We can also use the `symmetric_difference()` method with multiple sets:

```# Create three sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}

# Find the symmetric difference between the three sets
result_set = set1.symmetric_difference(set2, set3)

# Print the result set
print(result_set)
```

Output:

`{1, 5}`

In the above example, we created three sets (`set1`, `set2`, and `set3`) and used the `symmetric_difference()` method to find the symmetric difference between them. The resulting set (`result_set`) contains the elements that are in either of the three sets but not in all of them.

Example 3: Using symmetric_difference() method with strings

We can also use the `symmetric_difference()` method with strings:

```# Create two sets of strings
set1 = {'apple', 'banana', 'orange'}
set2 = {'banana', 'kiwi', 'pineapple'}

# Find the symmetric difference between the two sets
result_set = set1.symmetric_difference(set2)

# Print the result set
print(result_set)
```

Output:

`{'apple', 'kiwi', 'pineapple', 'orange'}`

In the above example, we created two sets of strings (`set1` and `set2`) and used the `symmetric_difference()` method to find the symmetric difference between them. The resulting set (`result_set`) contains the elements that are in either `set1` or `set2`, but not in both.

Use Cases

The `symmetric_difference()` method can be used in various scenarios where we need to compare two or more sets and find the elements that are unique to each set. Some of the use cases include:

• Comparing two or more lists of items to find the unique items in each list
• Comparing the results of two or more database queries to find the unique records in each query
• Comparing two or more sets of user permissions to find the unique permissions in each set

By using the `symmetric_difference()` method, we can easily find the elements that are in either of the sets but not in both, and perform further operations on them as needed.