set.isdisjoint()
set.isdisjoint(other) bool · Updated March 15, 2026 · Set Methods The .isdisjoint() method returns True if two sets have no common elements — that is, their intersection is empty. This is useful for checking whether two groups share any members without modifying either set.
Syntax
set.isdisjoint(other)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
other | iterable | — | Any iterable to check for common elements. Converted to a set internally. |
Returns
bool:Trueif the set has no elements in common withother,Falseotherwise.
Examples
Basic usage
Check if two sets share any elements:
even_numbers = {2, 4, 6, 8, 10}
odd_numbers = {1, 3, 5, 7, 9}
print(even_numbers.isdisjoint(odd_numbers))
# True
The even and odd numbers have nothing in common.
Shared elements
When sets overlap, .isdisjoint() returns False:
fruits = {"apple", "banana", "cherry"}
citrus = {"orange", "lemon", "banana"}
print(fruits.isdisjoint(citrus))
# False
Both sets contain "banana", so they’re not disjoint.
With lists
You can check against any iterable — it doesn’t have to be a set:
primary_colors = {"red", "blue", "yellow"}
secondary = ["green", "orange", "purple", "red"]
print(primary_colors.isdisjoint(secondary))
# False
The method converts the list to a set internally, so "red" (in the list) matches "red" (in the set).
Empty sets are always disjoint
An empty set has no elements, so it’s disjoint with everything:
empty = set()
colors = {"red", "blue", "green"}
print(empty.isdisjoint(colors))
# True
print(colors.isdisjoint(empty))
# True
Practical: checking user permissions
A common use case is checking if a user has access to certain features:
user_permissions = {"read", "write", "delete"}
admin_permissions = {"admin", "superuser", "delete"}
# Check if user has any admin permissions
has_admin_access = user_permissions.isdisjoint(admin_permissions)
print(f"User is purely user-level: {has_admin_access}")
# User is purely user-level: False
Practical: finding non-overlapping categories
Compare categories to find those that don’t overlap:
web_technologies = {"html", "css", "javascript", "react"}
backend_languages = {"python", "java", "go", "rust"}
if web_technologies.isdisjoint(backend_languages):
print("No overlap between web and backend")
else:
print("Some overlap exists")
# No overlap between web and backend
Note that full-stack technologies like “javascript” (also used server-side with Node.js) would change this result.
Common Patterns
Conditional logic based on overlap
Use .isdisjoint() to decide which code path to take:
user_tags = {"python", "javascript", "react"}
frontend_keywords = {"javascript", "react", "css", "html"}
backend_keywords = {"python", "java", "sql", "api"}
def recommend_path(user_tags):
if user_tags.isdisjoint(frontend_keywords | backend_keywords):
return "Explore both web and backend!"
elif user_tags.isdisjoint(frontend_keywords):
return "Try backend development!"
elif user_tags.isdisjoint(backend_keywords):
return "Explore frontend development!"
else:
return "You're a full-stack developer!"
print(recommend_path({"python", "java"}))
# Try backend development!
Filtering non-conflicting items
Find items that don’t conflict with a blocked list:
allowed_items = {"apple", "banana", "cherry", "date"}
blocked_categories = {"rotten", "unripe"}
# Individual items to check
items_to_check = ["apple", "date", "mango"]
for item in items_to_check:
# Treat single item as a set for comparison
item_set = {item}
if item_set.isdisjoint(allowed_items):
print(f"{item}: Not in allowed list")
elif item_set.isdisjoint(blocked_categories):
print(f"{item}: Allowed and not blocked")
else:
print(f"{item}: Blocked or unknown")
# apple: Allowed and not blocked
# date: Allowed and not blocked
# mango: Not in allowed list
Validating data segregation
Ensure two datasets don’t mix:
active_users = {"alice", "bob", "charlie"}
banned_users = {"charlie", "david"}
if active_users.isdisjoint(banned_users):
print("No banned users are currently active")
else:
print("Warning: Banned users are still active!")
common = active_users & banned_users
print(f"Conflict: {common}")
# Warning: Banned users are still active!
# Conflict: {'charlie'}
Performance
The .isdisjoint() method has:
- Average case: O(min(len(set1), len(set2))) — it stops early if it finds a common element
- Worst case: O(len(set1) + len(set2)) when no elements match
Python’s implementation is optimized to exit early as soon as it finds a shared element, making it efficient for both disjoint and overlapping sets.
See Also
- set::set.intersection() — returns common elements between sets
- set::set.difference() — returns elements in the set but not in another
- set::set.copy() — creates a shallow copy of the set