set.isdisjoint()
Test whether two collections share nothing — cleaner and often faster than checking `not (a & b)`.
Demo
isdisjoint returns True when the two sets share no elements — the intersection is empty. Empty vs anything is always True (an empty set cannot share anything). Neither input is modified. Unlike issubset / issuperset, there is no operator form: no `!&` or similar. The method is the only way.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| other | iterable | yes | A single iterable — set, list, tuple, generator, string, dict (keys). isdisjoint() accepts any iterable, and can short-circuit as soon as one common element is found. |
Return value
bool — True if self and other share NO elements — the intersection is empty. Empty set is disjoint from every set, including empty. No operator form; the method is the only way.
Common patterns
if not user_tags.isdisjoint(blocked_tags): deny()
if not required.isdisjoint(current): log("at least one required item is already present")
safe = [item for item in items if item.tags.isdisjoint(blacklist)]
Examples
Pitfalls
a &! b # not a thing
a.isdisjoint(b)
set().isdisjoint(set())
if a and b and a.isdisjoint(b): ... # both non-empty AND disjoint
if not (a & b): ... # builds a & b, then checks
if a.isdisjoint(b): ... # no allocation
{"abc"}.isdisjoint("abcd")
{"abc"}.isdisjoint({"abc", "d"})
When to use
- Access-control "no blocked tags" checks
- "Any overlap" questions where you do not need the overlap itself
- Short-circuiting on the first common element — cheaper than materializing the intersection
- Filtering compatible items via list comprehension
- You need the shared elements → set.intersection
- "Every element of self is in other" → set.issubset
- "Self contains every element of other" → set.issuperset
- One-liner readability when the intersection is the point → `not (a & b)` is more direct
Notes
FAQ
No. issubset has `<=`, issuperset has `>=`, but isdisjoint has no operator. The method is the direct expression — for a compound form, use `not (a & b)` (but that is slower and allocates).