how to check if two sets are disjoint in python

One way to check if two sets are disjoint in Python is by using the isdisjoint() method. The isdisjoint() method returns a boolean value indicating whether two sets have any common elements or not.

Here is an example:

main.py
set1 = {1, 2, 3}
set2 = {4, 5, 6}
set3 = {3, 4, 5}

print(set1.isdisjoint(set2))  # True
print(set1.isdisjoint(set3))  # False
127 chars
7 lines

In the above example, set1 and set2 do not have any common elements, so set1.isdisjoint(set2) returns True. On the other hand, set1 and set3 have a common element (i.e. 3), so set1.isdisjoint(set3) returns False.

gistlibby LogSnag