Python - Set isdisjoint() Method


Advertisements

The Python set isdisjoint() method is used to find out whether two sets have any common element or not. It returns true if there is no common element between two sets, else returns false. Please note that, this method can take iterable as an argument also.

Note: Two sets are called disjoint sets if there is no common element between them. The below figure depicts two sets called A = {10, 20, 30, 40, 50, 60} and B = {70, 80, 90}. As there is no common element between A and B, hence A and B are called disjoint sets.

Disjoint Sets

Syntax


set.isdisjoint(iterable)

Parameters

iterable Required. specify set or iterable which need to be compared with the given set.

Example:

In the below example, SetA is checked againt SetB, SetC, ListB and ListC for disjoint sets using python isdisjoint() method.

SetA = {10, 20, 30, 40, 50, 60}

SetB = {70, 80, 90}
SetC = {5, 10, 15, 20, 25}

ListB = [70, 80, 90]
ListC = [5, 10, 15, 20, 25]

print(SetA.isdisjoint(SetB))
print(SetA.isdisjoint(SetC))

print(SetA.isdisjoint(ListB))
print(SetA.isdisjoint(ListC))

Output

True
False

True
False

Sets Methods




Advertisements