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
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 |
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 |
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))
Output:
A and B disjoint? True A and C disjoint? False
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))
Output:
A and B disjoint? False A and C disjoint? False A and D disjoint? False A and E disjoint? True