Python Set isdisjoint() Method
The isdisjoint() method checks whether two sets have no elements in common. It returns True if the sets are disjoint (share no common elements), and False otherwise.
Syntax
set1.isdisjoint(set2)
Parameters
set2 (required): Another set (or any iterable) to compare with.
Return Value
Returns True if two sets have no elements in common.
Returns False otherwise.
Examples
Example 1: Sets with no common elements
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2)) # Output: True
Example 2: Sets with common elements
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.isdisjoint(set2)) # Output: False
Example 3: Empty set
Empty sets are always disjoint with any other set (including another empty set).
set1 = set() # Empty set
set2 = {3, 4, 5}
set3 = set() # Empty set
print(set1.isdisjoint(set2)) # Output: True
print(set1.isdisjoint(set3)) # Output: True
Working With Different Iterables
The isdisjoint() method also works with other iterables, such as lists and tuples.
set1 = {1, 2, 3}
list1 = [4, 5, 6] # a list
tuple1 = (7, 8, 9) # a tuple
print(set1.isdisjoint(list1)) # Output: True
print(set1.isdisjoint(tuple1)) # Output: True