Join Sets Using union() :
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Output :
{'a', 'b', 'c', 1, 2, 3}
Keep Only Duplicates Using intersection_update() :
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
Output :
{'apple'}
Get Intersection as New Set :
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
Output :
{'apple'}
Keep Only Non-Matching Items Using symmetric_difference update() :
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
Output :
{'banana', 'google', 'cherry', 'microsoft'}
Get Symmetric Difference as New Set :
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
Output :
{'banana', 'google', 'cherry', 'microsoft'}