Python Reference ยท Dictionaries and Sets
Python set.issuperset() method
Reference for Python set.issuperset(): check that a set contains every element of another collection, the >= and > operators, empty arguments and proper supersets.
The issuperset() method returns True if the set contains every element of another collection. It is the mirror image of issubset(): a.issuperset(b) and b.issubset(a) always give the same answer.
The operator forms are a >= b (superset) and a > b (proper superset, meaning a has everything in b plus at least one more element).
{1, 2, 3}.issuperset({1, 2}) # True
{1, 2, 3}.issuperset({1, 9}) # False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
set.issuperset(other) set >= other set > other
| Name | Description |
|---|---|
other |
Any iterable for the method; a set or frozenset for the operators. |
Return value: True if every element of other is in the set, otherwise False.
Raises: TypeError if other is not iterable (method) or not a set (operators).
Usage notes
- Everything is a superset of the empty set, including the empty set itself.
- Equal sets are supersets of each other, but not proper supersets. Use
>for the strict test. - The method accepts any iterable; the operators do not.
a >= [1]raisesTypeError. - Unhashable items fail.
.issuperset([[1]])raisesTypeError, because lists cannot be looked up in a set. - Empty strings are empty iterables, so
a.issuperset("")isTrue, while a non-empty string is treated as a collection of characters. - To see what is missing, subtract:
needed - havegives the elements that prevent the superset test from passing. - Does not modify the set.
Related
set.issubset(),set.isdisjoint(),set.difference()- Lesson: Sets