The set.issubset(other) method returns True if every element of the calling set is contained within the specified collection. If even one element is missing, it returns False.
Short-Circuiting & Operator Equivalence: In set theory notation, this represents AAA ⊆ B. You can also evaluate set containment using the <= operator for subsets or < for strict proper subsets. Like isdisjoint(), issubset() short-circuits instantly upon discovering the very first member absent from the target container.
The standard CPython signature for set.issubset() is defined as:
set_instance.issubset(other)
| Component | Type | Required? | Behavior & Description |
|---|---|---|---|
other |
Iterable[Hashable] |
Yes (exactly one) | The reference collection (set, list, tuple, dict keys, or generator) to test containment against. |
| Return Value | bool |
— | Returns True if every item in the caller set exists in other; otherwise False. |
| Best-Case Time | O(1) | — | Short-circuits immediately when the first inspected item is missing from other. |
| Worst-Case Time | O(len(caller)) | — | When the caller is a true subset, every element must be verified against other. |
| Auxiliary Space | O(1) | — | Zero memory allocations; tests membership directly without building intermediate sets. |
Evaluating subsets produces two outcomes: full containment (returning True) or incomplete containment where at least one element is missing (returning False).
When all elements of set B exist inside set A, B.issubset(A) evaluates to True:
A = {'x', 'y', 'z', 'a', 'b', 'c'}
B = {'x', 'y', 'z'}
# Every element of B ('x', 'y', 'z') is in A
is_contained = B.issubset(A)
print("Is B a subset of A?", is_contained)
Output:
Is B a subset of A? True
When set B contains even a single element absent from A, B.issubset(A) returns False:
A = {'x', 'y', 'z', 'a', 'b', 'c'}
B = {'x', 'y', 'z', 'k', 'l', 'm'}
# 'k', 'l', 'm' are missing from A → False
is_contained = B.issubset(A)
print("Is B a subset of A?", is_contained)
Output:
Is B a subset of A? False
Python provides mathematical comparison operators for set containment. Understanding the distinction between general subsets and proper subsets is essential:
| Mechanism | Mathematical Meaning | Condition When A == B | Allowed Argument Types |
|---|---|---|---|
A.issubset(B) |
AAA ⊆ B (Subset) | True | Any Iterable (lists, tuples, dicts, strings) |
A <= B |
AAA ⊆ B (Subset) | True | Set instances only |
A < B |
AAA ⊂ B (Proper / Strict Subset) | False (AAA must be strictly smaller than ABA) | Set instances only |
s1 = {1, 2}
s2 = {1, 2}
s3 = {1, 2, 3}
print("s1 <= s2 (Equal sets are subsets): ", s1 <= s2) # True
print("s1 < s2 (Equal sets are NOT proper subsets):", s1 < s2) # False
print("s1 < s3 (s1 is strictly smaller than s3): ", s1 < s3) # True
Output:
s1 <= s2 (Equal sets are subsets): True
s1 < s2 (Equal sets are NOT proper subsets): False
s1 < s3 (s1 is strictly smaller than s3): True
Unlike comparison operators which mandate set operands, issubset() can evaluate lists, tuples, dictionaries, and character strings directly.
Pass a list directly to check if a set's items are all included in that list:
required_flags = {'x', 'y'}
system_flags = ['a', 'b', 'x', 'y']
print("Are required flags present in system list?", required_flags.issubset(system_flags))
Output:
Are required flags present in system list? True
When testing against a string, Python iterates over every character in that string:
char_set = {'y', 't', 'W', 'o', 'e'}
sentence = 'Welcome to Python'
# 'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n'
print("Are all characters in sentence?", char_set.issubset(sentence))
# To test entire words, wrap them in a container!
needed_terms = {"python", "django"}
available_tags = ["python", "django", "docker"]
print("Are words a subset of tags? ", needed_terms.issubset(available_tags))
Output:
Are all characters in sentence? True
Are words a subset of tags? True
Validate that a user's requested API operations do not exceed their assigned permission scope:
def validate_api_action(requested_scopes: set[str], user_role_scopes: set[str]) -> bool:
return requested_scopes.issubset(user_role_scopes)
developer_entitlements = {"read_code", "push_branch", "view_ci_logs"}
action_1 = {"read_code", "view_ci_logs"}
action_2 = {"push_branch", "deploy_production"}
print("Action 1 authorized:", validate_api_action(action_1, developer_entitlements))
print("Action 2 authorized:", validate_api_action(action_2, developer_entitlements))
Output:
Action 1 authorized: True
Action 2 authorized: False
Ensure that an incoming JSON payload does not inject unauthorized or unexpected database fields:
ALLOWED_USER_FIELDS = {"username", "email", "bio", "location"}
def is_payload_safe(incoming_payload: dict) -> bool:
# In Python, set(dict) or dict.keys() extracts keys for inspection
return set(incoming_payload).issubset(ALLOWED_USER_FIELDS)
payload_safe = {"username": "alice", "email": "alice@example.com"}
payload_malicious = {"username": "bob", "is_admin": True}
print("Safe payload valid: ", is_payload_safe(payload_safe))
print("Malicious payload valid:", is_payload_safe(payload_malicious))
Output:
Safe payload valid: True
Malicious payload valid: False
set().issubset(anything) is always True:
print(set().issubset({1, 2})) # True
print(set().issubset(set())) # True (Ø ⊆ Ø)
<= operator strictly requires a set instance on the right-hand side. Passing a list raises a TypeError:
s = {1, 2}
try:
result = s <= [1, 2, 3]
except TypeError as e:
print("Operator error:", e) # unsupported operand type(s) for <=: 'set' and 'list'
Validate your understanding of subset behaviors across edge cases with this self-verifying test suite:
# Test 1: Reflexive property (every set is a subset of itself)
s = {10, 20, 30}
assert s.issubset(s) is True
assert (s <= s) is True
assert (s < s) is False # Not a strict proper subset
# Test 2: Empty set containment
assert set().issubset(s) is True
assert (set() < s) is True # Empty set is a proper subset of non-empty set
# Test 3: Heterogeneous iterables
assert {'a', 'b'}.issubset(['a', 'b', 'c']) is True
assert {'a', 'z'}.issubset(['a', 'b', 'c']) is False
# Test 4: Dictionary key evaluation
d = {"key1": 1, "key2": 2}
assert {"key1"}.issubset(d) is True
assert {1}.issubset(d) is False # Checks keys, not values
print("All set.issubset assertions passed successfully!")
Output:
All set.issubset assertions passed successfully!
Deepen your skills with live interactive examples, proper subset comparisons, and RBAC authorization drills in our companion notebook:
Explore interactive code cells, benchmark issubset() vs. <=, and solve real-world entitlement verification exercises.
Tests whether the calling set contains every element of another collection (superset containment).
Tests whether two sets share zero common elements (zero shared elements).
Computes the set of common elements, returning a new set object.
Computes elements unique to the calling set, excluding items found in other iterables.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.