isdisjoint(other) answers one question: do the set and other have nothing in common? It returns True if they share no item, and False as soon as it finds one that is shared.
Nothing is changed or built, so it is the cheapest way to test for an overlap. The argument can be any iterable, such as a list, a tuple, or a string.
set.isdisjoint() is an instance method of the built-in set type: call it on a set with dot notation, such as room_a.isdisjoint(...). No import is needed.
Syntax
Pass one iterable and use the returned True or False in a condition.
python
no_overlap = s.isdisjoint(iterable)
Call
What it does
{1, 2}.isdisjoint({3, 4})
Returns True: no item appears in both.
{1, 2}.isdisjoint([2, 5])
Returns False: 2 is in both. Any iterable can be used.
set().isdisjoint({1})
Returns True: an empty set shares nothing with anything.
a.isdisjoint(b) gives the same answer as not (a & b), but it stops at the first shared item and builds no new set. To see which items are shared, use intersection().
Method signatures
python
set.isdisjoint(other)
# returns: True if the set and other share no item, otherwise False
# raises: TypeError if other is not iterable
Parameters
Parameter
Description
other
Any iterable: set, list, tuple, string, range, dict (its keys), and so on. It is required: isdisjoint() with no argument raises TypeError.
Return Value
Return Value
Description
bool
True if the two collections have no item in common; False if at least one item is shared.
Python Examples
Example 1 — Check Two Meetings Do Not Overlap
Store the hours each room is busy and test whether bookings clash.