How to check if two sets are disjoint in Python
Today I learned that Python has built-in functions to assess whether two sets are disjoints (i.e. have no elements in common).
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.isdisjoint(set2))
False
Alternatively, you can use the intersection()
function to find the points of intersection between two sets:
intersects = set1.intersection(set2)
print(intersects)
{3, 4}