Creating a Difference of Sets

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

Example 1:

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

Example 2:

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

The difference() method in Python returns a set that is the difference between two sets.

def set_difference(set1, set2):
    return set1.difference(set2)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!