Python Set issuperset() Method
The issuperset() method checks whether a set contains all elements of another set (or iterable). It returns True if the set is a superset of the specified set (or iterable), and False otherwise.
Syntax
set1.issuperset(set2)
Parameters
set2 (required): The set or iterable to compare against.
Return Value
Returns True if the set contains all elements of the other set.
Returns False otherwise.
Examples
Example 1: Check if one set contains all elements of another
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
set3 = {1, 2, 9}
print(set1.issuperset(set2)) # Output: True
print(set1.issuperset(set3)) # Output: False
Example 2: Equal sets
A set is a superset of itself.
set1 = {1, 2, 3}
set2 = {1, 2, 3}
print(set1.issuperset(set2)) # Output: True
Example 3: With different iterable types
The issuperset() method also works with iterables of different types, such as lists or tuples.
set1 = {1, 2, 3, 4, 5}
list1 = [1, 2, 3] # a list
tuple1 = (1, 2, 5) # a tuple
print(set1.issuperset(list1)) # Output: True
print(set1.issuperset(tuple1)) # Output: True
Using The >= Operator
You can also use the >= operator to check whether one set is a superset of another.
For example:
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
print(set1 >= set2) # Output: True
Proper Superset
A superset is a set that contains all elements of another set and is strictly larger. The two sets cannot be equal.
For example:
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
set3 = {1, 2, 3, 4, 5}
print(set1 > set2) # Output: True
print(set1 > set3) # Output: False