inspection
set.isdisjoint
Returns True if s and other share no elements. Accepts any iterable; there is no operator form.
s.isdisjoint(other) Parameters
| Parameter | Purpose |
|---|---|
| other | Any iterable to test against s |
| returns | bool — True when the intersection is empty |
Examples
>>> {1, 2}.isdisjoint({3, 4})
True No common elements.
>>> {1, 2}.isdisjoint({2, 3})
False Element 2 is shared.
>>> {1, 2}.isdisjoint([3, 4, 5])
True Accepts any iterable, not only sets.
>>> set().isdisjoint({1, 2})
True The empty set is disjoint from every set.
Gotcha
There is no & or | style operator for disjointness. isdisjoint is faster than 'not (a & b)' because it can short-circuit on the first shared element.
Related methods
set.intersection
Returns a NEW set containing only elements found in s AND in every argument. Method form accepts any iterables; & requires sets.
set.issubset
Returns True if every element of s is also in other. Method form accepts any iterable; <= requires a set. Use < for strict (proper) subset.
set.issuperset
Returns True if every element of other is in s. Method form accepts any iterable; >= requires a set. Use > for strict (proper) superset.
← All Python set methods · List methods · list vs tuple vs set