![]() |
VOOZH | about |
The set.symmetric_difference() method in Python returns a new set containing elements that are present in either of the two sets but not in both. It removes common elements and keeps only unique elements from each set. The original sets remain unchanged.
Example: In this example, elements that are present in either set but not common to both are found using symmetric_difference().
{1, 2, 5, 6, 7, 9}
Explanation: a.symmetric_difference(b) removes common elements 3 and 4. It returns elements unique to each set.
set1.symmetric_difference(set2)
Example 1: Here, symmetric difference is calculated between a set and a list.
{1, 2, 3, 4, 8, 9}
Explanation:
Example 2: In this example, the ^ operator is used as a shortcut for symmetric difference.
{'J', 'G'}
Explanation: a ^ b performs symmetric difference, common elements are removed.
Example 3: In this example, symmetric difference is performed when one set is empty to observe the result.
{10, 20, 30}
{10, 20, 30}
Explanation: