# symmetric_difference() Python

## Overview

The symmetric difference in python can be found by using the symmetric_difference() method. This method applies to sets and returns the set which contains all the elements of both sets excluding the elements that are common in both the set

## Syntax of symmetric_difference() in Python

Syntax of symmetric_difference() method in python is given below:

where A and B are two sets whose symmetric difference we want to find

## Parameters of symmetric_difference() in Python

The symmetric_difference() method in python takes a single parameter as an input which is given below:

**Set**: symmetric_difference() takes set as a parameter to perform symmetric difference. symmetric_difference() can accept iterable or a set as parameter. Then the symmetric difference operation is performed on the set and the elements of symmetric difference will be returned in the output set.

## Return Value of symmetric_difference() in Python

Return value of symmetric_difference() method in python is given below:

**Set**: It returns a set which contains the elements of both sets excluding the elements that are common in both the sets.

## Example of symmetric_difference() in Python

**Output**

## What is symmetric_difference() in Python?

symmetric_difference() in python is used to perform symmetric difference operations on a set. It will take a single parameter set as an input and return the set that contains the elements of both sets excluding the intersection elements of both sets. You can pass iterable such as list, tuple, dictionary, string, etc. or a set as a parameter in symmetric_difference(). First of all it will convert iterable into set if it is not a set then performs symmetric difference operation with the set. In symmetric difference in python it will take the elements that are present in either of the two sets but not present in both the sets.

**Note:**

- Single parameter can be passed in the symmetric_difference().
- Parameter of symmetric_difference() can be iterable or a set.

- In place of set2 you can pass iterable such as list, tuple, dictionary, string, etc. or a set as a parameter.
- But in place set1 you have to use set

## More Examples

**Example 1: Applying symmetric_difference() on string set**

**Output**

**Example 2: Applying symmetric_difference() on iterable**

**Output**

## Using the symmetric_difference() Method to Find the Symmetric Difference of Sets

The symmetric_difference() method is used to perform a symmetric difference operation on a set. Symmetric difference in python, will include those elements of both sets which are present in either set but not present in both the sets.

## Symmetric Difference Using ^ Operator

^ in python is also used to find the symmetric difference of sets. It will return the set of all the elements of the set except the elements that are present in both sets.
^ operator can be used to find symmetric difference of two or more sets simultaneously.
**Example:**

**Output**

## The symmetric_difference() Method vs Symmetric Difference Operator (^)

symmetric_difference() | ^ operator |
---|---|

symmetric_difference() can perform symmetric difference on iterables. | ^ operator does not perform symmetric difference on iterables. |

symmetric_difference() can be applied on two sets. | ^ operator can be applied on more than two sets. |

Example: set1.symmetric_difference(set2) | Example: set1^set2 |

symmetric_difference() is used to find a symmetric difference in python that accepts iterable as a parameter which may be tuple, list, set, string or dictionary.

And convert the accepted iterable into the set and find the symmetric difference and returns the set as an output which contains the symmetric difference.

**Example of symmetric_difference()**

**Output:**

The ^ operator can only be used with sets.
^ operator can only be applied on sets.
If we try to apply the ^ operator on a string, tuple, list, set or dictionary then you will get an error.
^ operator applies symmetric difference on two or more sets and returns set as an output.
**Example**

**Output**

## Conclusion

- symmetric_difference() is a method used on sets for finding the symmetric difference in python.
- This method takes a set as a parameter.
- It will return a set consisting of elements that are in either set excluding the elements that are common in both sets.
- ^ operator also finds the symmetric difference in python.
- symmetric_difference() can take iterable as input but ^ operator works only on sets.