set.issubset() / set.issuperset() / set.isdisjoint()
Methods for checking the subset relationship between sets. You can determine whether one set is a subset or superset of another, or whether two sets share no common elements.
Syntax
# Check whether set A is a subset of set B. setA.issubset(setB) setA <= setB # Check whether set A is a proper subset of set B. setA < setB # Check whether set A is a superset of set B. setA.issuperset(setB) setA >= setB # Check whether set A and set B have no elements in common. setA.isdisjoint(setB)
Method List
| Method / Operator | Description |
|---|---|
| setA.issubset(setB) / <= | Returns True if every element of set A is contained in set B. Also returns True if A and B are equal. |
| setA < setB | Returns True if set A is a proper subset of set B (A is contained in B, and A and B are not equal). |
| setA.issuperset(setB) / >= | Returns True if every element of set B is contained in set A. Also returns True if A and B are equal. |
| setA > setB | Returns True if set A is a proper superset of set B (B is contained in A, and A and B are not equal). |
| setA.isdisjoint(setB) | Returns True if set A and set B share no common elements. |
Sample Code
# Prepare some sets.
all_permissions = {'read', 'write', 'execute', 'delete'}
user_permissions = {'read', 'write'}
guest_permissions = {'read'}
# issubset(): Check for a subset relationship.
print(user_permissions.issubset(all_permissions)) # True: user is a subset of all
print(user_permissions <= all_permissions) # Also True
# Equal sets are also considered subsets of each other.
print(all_permissions <= all_permissions) # True
# Check for a proper subset (a subset that is not equal).
print(user_permissions < all_permissions) # True
print(all_permissions < all_permissions) # False: equal sets are not proper subsets
# issuperset(): Check for a superset relationship.
print(all_permissions.issuperset(user_permissions)) # True
print(all_permissions >= user_permissions) # Also True
# isdisjoint(): Check whether two sets share no common elements.
admin_only = {'delete', 'execute'}
print(guest_permissions.isdisjoint(admin_only)) # True: no common elements
print(user_permissions.isdisjoint(admin_only)) # False: 'write' is not shared, but check carefully
# Verify with a direct example.
print({'write'}.isdisjoint({'execute', 'delete'})) # True: no common elements
# Practical example: check whether a user has all required permissions.
required = {'read', 'write'}
has = {'read', 'write', 'execute'}
if required.issubset(has):
print('All required permissions are present.')
Notes
issubset() checks whether "A is entirely contained within B", while issuperset() checks whether "A entirely contains B". Both return True when the two sets are equal. If you want to exclude the equal case, use the < or > operators (proper subset / proper superset).
isdisjoint() returns True when the two sets share no elements in common. It is equivalent to setA & setB == set(), but isdisjoint() is more efficient.
The methods accept any iterable (such as a list) as an argument, but the operators (<=, >=, <, >) require both operands to be sets.
For set operations such as union, intersection, and difference, see set.union() / set.intersection() / set.difference().
If you find any errors or copyright issues, please contact us.