set.isdisjoint()

Test whether two collections share nothing — cleaner and often faster than checking `not (a & b)`.

Set methodPython 2.6+Live demo
Common call
if blocked.isdisjoint(user_tags):
Returns
True or False
Replaces
`not (a & b)` — but isdisjoint can short-circuit on the first shared element
Watch out
no operator form; empty set is disjoint from EVERYTHING (including empty)
set.isdisjoint(otherotherA 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.type: iterable · required)
bool

Demo

Live evaluation
Try:
Inputs
asetfirst set (comma-separated)
bsetsecond set (comma-separated)
Output
{'1', '2', '3'}.isdisjoint({'4', '5', '6'})
TypeError: 'object' object is not iterable

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

NameTypeRequiredDescription
otheriterableyesA 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

boolTrue 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

Access-control check
Reject if the user has any blocked tag.
if not user_tags.isdisjoint(blocked_tags):
    deny()
"Any overlap" early exit
isdisjoint short-circuits on the first shared element — cheaper than materializing `a & b`.
if not required.isdisjoint(current):
    log("at least one required item is already present")
Filter compatible items
Keep only items whose tags do NOT overlap with a blacklist.
safe = [item for item in items if item.tags.isdisjoint(blacklist)]

Examples

1. Disjoint
{1, 2, 3}.isdisjoint({4, 5, 6})
Returns
True
2. Overlap
{1, 2, 3}.isdisjoint({3, 4, 5})
Returns
False
3. Empty is universal
set().isdisjoint({1, 2, 3})
Returns
True
4. Empty vs empty
set().isdisjoint(set())
Returns
True
5. Iterable other
{1, 2, 3}.isdisjoint([4, 5])
Returns
True
6. Single shared
{"a", "b"}.isdisjoint(["c", "b"])
Returns
False # "b" is shared

Pitfalls

1. No operator form — it is a method only
issubset has `<=`, issuperset has `>=`, but isdisjoint has NO equivalent operator. The method is the only way to express it directly.
No operator
a &amp;! b   # not a thing
SyntaxError
Use the method
a.isdisjoint(b)
True or False
2. Empty set is disjoint from EVERYTHING
Sharing requires membership on both sides. The empty set has no members, so it cannot share anything — even with another empty set. This is vacuously true and can be surprising.
Unexpected True
set().isdisjoint(set())
True
Guard for empty
if a and b and a.isdisjoint(b):
    ...   # both non-empty AND disjoint
non-trivial disjoint
3. isdisjoint is CHEAPER than checking `not (a &amp; b)`
The intersection form materializes the whole intersection before checking emptiness. isdisjoint iterates one side and short-circuits on the first shared element — no allocation, faster on hits.
Materializes intersection
if not (a &amp; b):
    ...   # builds a &amp; b, then checks
allocates a set
Short-circuits
if a.isdisjoint(b):
    ...   # no allocation
faster on hits
4. String iterables explode into characters
Same footgun as every other set method — a string passed as other is iterated as characters. Multi-char items in self will not match individual characters in a string.
Char comparison
{"abc"}.isdisjoint("abcd")
True # "abc" is not one of the chars
Wrap it
{"abc"}.isdisjoint({"abc", "d"})
False

When to use

Use it
  • Access-control &quot;no blocked tags&quot; checks
  • &quot;Any overlap&quot; 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
Reach for something else
  • You need the shared elements → set.intersection
  • &quot;Every element of self is in other&quot; → set.issubset
  • &quot;Self contains every element of other&quot; → set.issuperset
  • One-liner readability when the intersection is the point → `not (a & b)` is more direct

Notes

Complexity
O(min(|a|, |b|)) worst case — iterates the smaller and probes the larger; short-circuits on the first shared element
Return
bool — True or False
CPython impl
Objects/setobject.c :: set_isdisjoint
Memory
No allocation for sets; other may be materialized if it is a plain iterable
Thread-safe
Safe against reads; not safe under concurrent writes to either input

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).

History

2.6
isdisjoint() introduced.