set.issuperset()
Returns True when this set contains every element of the other collection. Same as the >= operator for two sets.
set.issuperset(other)a.issuperset(b) is the mirror of set.issubset(): it asks whether a contains everything that is in b. Extra elements in a are fine.
Pick whichever reads more naturally. "Does my toolbox contain all these tools?" reads well as toolbox.issuperset(needed).
A set is a superset of itself. For "contains everything and at least one more", use the operator a > b.
Parameters
| Parameter | What it is |
|---|---|
otherrequired | A set or other iterable whose elements must all be in this set. |
Return value
True or False.
Examples
1Basic check
Python
big = {1, 2, 3, 4}
print(big.issuperset({2, 3}))
print(big.issuperset({3, 5}))
print(big >= {1, 4})Output
True False True
2Does a password use every kind of character?
Python
import string
kinds = {"lower", "upper", "digit"}
for pw in ["sunny42", "Sunny42"]:
found = set()
for ch in pw:
if ch in string.ascii_lowercase:
found.add("lower")
elif ch in string.ascii_uppercase:
found.add("upper")
elif ch in string.digits:
found.add("digit")
print(pw, found.issuperset(kinds))Output
sunny42 False Sunny42 True
3Superset vs proper superset
Python
a = {1, 2, 3}
print(a.issuperset({1, 2, 3}))
print(a > {1, 2, 3})
print(a > {1, 2})Output
True False True
Common mistakes
Using >= with a list
The operator needs two sets; the method accepts any iterable.
Python
toolbox = {"hammer", "saw", "drill"}
needed = ["saw", "drill"]
print(toolbox.issuperset(needed))
try:
toolbox >= needed
except TypeError:
print("TypeError: >= needs two sets")Output
True TypeError: >= needs two sets
Questions people ask
What is the difference between issuperset() and issubset()?
They look from opposite sides: a.issuperset(b) is the same as b.issubset(a) when both are sets.
How do I check that a list contains all required values?
set(items).issuperset(required).
Related
set.issubset()Returns True when every element of this set is also in the other collection. Same as the <= operator for two sets.set.isdisjoint()Returns True when this set and the other collection have no element in common, and False as soon as one is shared.set.union()Returns a new set with every element that is in this set or in any of the others, each one only once. Same as the | operator.set.difference()Returns a new set with the elements of this set that are not in any of the others. Same as the - operator.