The issuperset()
method is used to check whether a set is a super set of another set given as argument.
A set is a super set if it contains all the elements of the other set and possibly more. For example, set {1, 2, 3, 4, 5}
is a super set of both, {2, 4}
and {1, 3, 5}
as it contains all the elements of both sets.
Logically, any set its own super set.
set1.issuperset(set2)
set2 |
The set to check whether this set is a super set of. |
The method returns True
if the set is a super set of the given set2
, else False
.
integers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
odds = {1, 3, 5, 7, 9}
evens = {0, 2, 4, 6, 8}
#check whether set integers is a supers set
print(integers.issuperset(integers)) #it is its own superset
print(integers.issuperset(odds))
print(integers.issuperset(evens))
In the above example, the issuperset()
method returns True
in all cases because the integers
set is a super set of all the sets passed as arguments including itself.
odds = {1, 3, 5, 7, 9}
evens = {2, 4, 6, 8}
print(odds.issuperset(evens))
print(evens.issuperset(odds))
In the above snippet, the issuperset()
method returns False in both case.
Using the >=
operator instead
The >=
operator is the equivalent operator version of the superset()
method. It also determines if a set is a super set of another set.
set1 >= set2
It returns True
if set1
is a superset of set2
, else False
.
integers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
odds = {1, 3, 5, 7, 9}
evens = {0, 2, 4, 6, 8}
#returns True
print(integers >= odds)
print( integers >= evens)
#returns False
print(odds >= integers)
print(odds >= evens)
print(evens >= odds)
Check if a set is a strict super set
A strict super set is a set that contains all elements of another set, but there must be at least one more additional element in the super set. It is also known as a proper super set.
A set is not its own strict super set.
To check whether a set s a strict super set, we use the >
operator.
set1 > set2
The operation returns True
if set1
is a strict super set of set2
.
integers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
odds = {1, 3, 5, 7, 9}
evens = {0, 2, 4, 6, 8}
print(integers > integers) #it is NOT its own strict superset
print(integers > odds)
print(integers > evens)