Introduction
Disjoint sets, also known as mutually exclusive sets, are sets that have no element in common. In Python, you can check whether sets are disjoint using the isdisjoint()
instance or class method.
Testing for disjoint sets is valuable in scenarios where you need to ensure that two sets don’t share any elements. For example, in a scheduling application, you might want to make sure that the time slots of two events are disjoint, indicating they don’t overlap. Another example is ensuring that the ingredients of a food item don’t include ingredients a person is allergic to.
Using the isdisjoint() Instance Method
The isdisjoint()
instance method of Python set
objects checks whether the calling set and another iterable have no elements in common. It returns True
if they don’t, i.e. they are disjoint, and False
otherwise.
Example:
set_a = {1, 2, 3} set_b = {4, 5, 6} result = set_a.isdisjoint(set_b) print(result)
Output:
True
In the example above, set_a
and set_b
are disjoint since they have no common elements, resulting in True
.
Example:
set_a = {1, 2, 3} set_b = {3, 4, 5} result = set_a.isdisjoint(set_b) print(result)
Output:
False
Here, set_a
and set_b
are not disjoint because they share the element 3
, leading to False
.
The following example demonstrates that isdisjoint()
works with other iterable types, e.g. a list.
Example:
set_a = {1, 2, 3} lst = [4, 5, 6] result = set_a.isdisjoint(lst) print(result)
Output:
True
The above example tests whether the set set_a
and the list lst
are disjoint. Except for issubset()
‘s argument being a list and not a set, it is identical to the first example.
Using the isdisjoint() Class Method
Similar to the instance method, the isdisjoint()
class method allows you to check whether a set, given as the first argument, has no elements in common with an iterable, given as the second.
The difference between the class method and instance method version of isdisjoint()
is that the instance method is called on a specific set
object, whereas the class method includes the set
object as its first parameter. Following the first argument, which must be a set, the second argument can be objects of any iterable type.
Since this version of isdisjoint()
is a class method of the set
class, you call it with a set.
prefix.
Example:
set_a = {1, 2, 3} lst = [4, 5, 6] result = set.isdisjoint(set_a, lst) print(result)
Output:
True
This achieves the same result as the previous example but using the class method.
Summary & Reference for Python Disjoint Set Operations
Python disjoint set operations allow you to determine whether two sets contain no elements in common.
The isdisjoint()
instance method checks whether the calling set and another iterable object have no common elements. It returns True
if they don’t, and False
otherwise.
set_a = {1, 2, 3} lst = [4, 5, 6] result = set_a.isdisjoint(lst) # result --> True
The
class method, an alternative to the instance method, is called directly on the isdisjoint
()set
class. Its first argument must be a set, and the second can be an object of any iterable type.
set_a = {1, 2, 3} lst = [4, 5, 6] result = set.isdisjoint(set_a, lst) # result --> True