Introduction
We use subset and superset checks to see if all items of one group are inside another group or if one group contains all items of another. This helps us compare collections easily.
Jump into concepts and practice - no test required
set1.issubset(set2) set1.issuperset(set2) # Or using operators: set1 <= set2 # subset check set1 >= set2 # superset check
a = {1, 2}
b = {1, 2, 3}
print(a.issubset(b)) # True
print(b.issubset(a)) # Falsex = {4, 5, 6}
y = {5, 6}
print(x.issuperset(y)) # True
print(y.issuperset(x)) # Falses1 = {10, 20}
s2 = {10, 20, 30}
print(s1 <= s2) # True
print(s2 >= s1) # Truefriends_invited = {"Alice", "Bob", "Charlie"}
party_guests = {"Alice", "Bob", "Charlie", "David"}
# Check if all invited friends are in the guest list
all_invited_coming = friends_invited.issubset(party_guests)
# Check if guest list includes all invited friends
guest_list_covers_invited = party_guests.issuperset(friends_invited)
print(f"All invited friends coming? {all_invited_coming}")
print(f"Guest list covers all invited? {guest_list_covers_invited}")set_a = {1, 2, 3}
set_b = {1, 2, 3, 4, 5}
print(set_a <= set_b)
print(set_b >= set_a)set_a = {1, 2}
set_b = {1, 2, 3}
result = set_a.subset(set_b)
print(result)set_x = {2, 4, 6, 8}
set_y = {4, 6}True for checking if set_y is a proper subset of set_x (subset but not equal)?< operator which checks proper subset (subset but not equal). set_y >= set_x checks superset (wrong direction). set_x.issubset(set_y) reverses the sets (wrong). set_y.issuperset(set_x) is wrong (issuperset).