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.py127 chars7 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