Creating Symmetric Difference of Sets

Write a Python program to create a symmetric difference of two sets. The function should return the symmetric difference.

Example 1:

Input: {1, 2, 3, 4}, {3, 4, 5, 6} 
Output: {1, 2, 5, 6}

Example 2:

Input: {"apple", "banana", "cherry"}, {"banana", "cherry", "dates"} 
Output: {"apple", "dates"}

The symmetric_difference() method returns a set that contains all items from both sets, but not the duplicate items.

def set_symmetric_difference(set1, set2):
    return set1.symmetric_difference(set2)

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!