Set Subsets, Supersets and Disjointness
Core idea
a.issubset(b) or a <= b checks whether every member of a belongs to b, including when both are equal. a < b requires a strict subset: b must also contain at least one additional member. b.issuperset(a) expresses the same non-strict containment from the other direction. a.isdisjoint(b) checks that there are no shared members.
required = {"saw", "ruler"}available = {"saw", "ruler", "pen"}print(required <= available)print(available.issuperset(required))All required tools are present.
Distinguish equal and proper subsets
a = {2, 4}b = {4, 2}print(a <= b)print(a < b)The groups are equal; neither is a strict subset.
Check empty groups
empty = set()print(empty.issubset({1, 2}))print(empty.isdisjoint({1, 2}))print({2, 3}.isdisjoint({3, 4}))The empty set has no missing required members and no shared members. The last groups share 3.
Common mistake
For sets, < does not compare the number of members alone. {1} < {2, 3} is False because 1 is absent from the second set.
Practice
Check whether all requested materials are available. Then test an empty request and two equal groups.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Subset and disjointness
Check your understanding of subset and disjointness.
What is printed?
print({1} <= {1})What is printed?
print({1} < {1})What is printed?
print(set().isdisjoint({1}))Which checks that available contains every required member?