Python Set isdisjoint() Method

The set.isdisjoint() method returns true if the given sets have no common elements. Sets are disjoint if and only if their intersection is the empty set.

set.isdisjoint() Syntax:

set.isdisjoint(other_set)

Parameters:

other_set: Required. The set to checked for being a disjoint set with set A.

Return Value:

Returns True if the sets are disjoin. False if the sets are not disjoint.

The following example demonstrates the set.isdisjoint() method.

Example: isdisjoint()
nums = {1, 2, 3, 4, 5 }
oddNums = {1, 3, 5, 7, 9}
primeNums = {7, 11, 13}

print(nums.isdisjoint(oddNums))
print(nums.isdisjoint(primeNums))
Output
False
True

Above, nums.isdisjoint(oddNums) returns True because they have common elements, whereas nums.isdisjoint(primeNums) returns False because they don't have common elements, so they are disjoint.

The set.isdisjoint() method can take other iterable types as an argument such as list, string, dictionary, and tuple.

Example: isdisjoint()
char_set = {'a','b','c','d','e'}
char_list = ['b','c','d']
char_str = 'ghij'
char_dict = {'a':1,'b':2}
char_tuple = ('x', 'y', 'z')

print(char_set.isdisjoint(char_list))
print(char_set.isdisjoint(char_str))
print(char_set.isdisjoint(char_dict))
print(char_set.isdisjoint(char_tuple))
Output
False
True
False
True
Want to check how much you know Python?