Skip to main content

isdisjoint()

The isdisjoint() function in Python is a Set Method that returns True if two sets are disjoint, meaning they have no elements in common. If there are common elements, it will return False.

Parameter Values

Parameter Description
other

The set or iterable to compare with the calling set for disjointness. It can be another set, list, tuple, or any iterable.

Return Values

The isdisjoint() method returns a bool indicating whether two sets have no elements in common.

How to Use isdisjoint() in Python

Example 1:

The isdisjoint() method returns True if two sets have no elements in common, otherwise it returns False.

set1 = {1, 2, 3, 4}
set2 = {5, 6, 7}
print(set1.isdisjoint(set2)) # Output: True
Example 2:

This method can be used with any iterable type, not just sets.

set1 = {1, 2, 3, 4}
list1 = [5, 6, 7]
print(set1.isdisjoint(list1)) # Output: True
Example 3:

It efficiently compares two sets to determine if they have any common elements.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.isdisjoint(set2)) # Output: False