Tutorial Study Image

Python isdisjoint()

The isdisjoint() function in python helps to check the given two sets are disjoint or not. It returns true if the sets are disjoint else it returns false. The disjoint means the two sets do not have common elements.

set_a.isdisjoint(set_b) #where parameter may be list, tuple, dictionary, and string

isdisjoint() Parameters:

The isdisjoint() function takes a single parameter. This method will automatically convert the given iterable parameters to a set for checking whether it is disjoint or not.

Parameter Description Required / Optional
set The set to search for equal items in Required

isdisjoint() Return Value

This method returns a boolean value true or false. We can also say that if the intersection of two sets is an empty set it will be a disjoint set.

Input Return Value
If sets are disjoint True
If sets are not disjoint False

Examples of isdisjoint() method in Python

Example 1: How isdisjoint() works in Python?

A = {'a', 'b', 'c', 'd'}
B = {'e', 'f', 'g'}
C = {'b', 'h', 'i'}

print(' A and B disjoint?', A.isdisjoint(B))
print(' A and C disjoint?', A.isdisjoint(C))


 A and B disjoint? True
 A and C disjoint? False

Example 2: Working of  isdisjoint() with other iterables as arguments

A = {1, 2, 3, 4}
B = [5, 3, 6]
C = '3mn7'
D ={1 : 'a', 2 : 'b'}
E ={'a' : 1, 'b' : 2}

print('A and B disjoint?', A.isdisjoint(B))
print('A and C disjoint?', A.isdisjoint(C))
print('A and D disjoint?', A.isdisjoint(D))
print('A and E disjoint?', A.isdisjoint(E))


A and B disjoint? False
A and C disjoint? False
A and D disjoint? False
A and E disjoint? True