The Set issuperset() method returns True if all items in the specified set are present in the original set. Otherwise, it returns False.
Syntax
my_set.issuperset(other_set)issuperset() Parameters
Python Set issuperset() method parameters:
| Parameter | Condition | Description |
|---|---|---|
other_set |
Required | A set |
issuperset() Return Value
Python Set issuperset() method returns
Trueif setmy_setis a superset ofother_setFalseif setmy_setis not a superset ofother_set
Examples
Example 1: Check if a Set is superset of Another Set
If set A is superset of B, then issuperset() returns True:
A = {'Tom', 'David', 'Anna'}
B = {'Anna', 'David'}
print(A.issuperset(B)) # Output: Trueoutput
TrueIf set A is not superset of B, then issuperset() returns False:
A = {'Anna', 'David'}
B = {'Tom', 'David', 'Anna'}
print(A.issuperset(B)) # Output: Falseoutput
FalseExample 2: Check if a Set is superset of a List
You can also pass iterables like list: issuperset() method first converts the iterables into sets and then verifies if the first set is a superset of the second one.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ['a', 'e']
C = ['a', 'b', 'c', 'd']
print('A is superset of B:', A.issuperset(B)) # Output: A is superset of B: True
print('A is superset of C:', A.issuperset(C)) # Output: A is superset of C: Falseoutput
A is superset of B: True
A is superset of C: FalseExample 3: Check if a Set is superset of a Tuple
You can also pass iterables like tuple: issuperset() method first converts the iterables into sets and then verifies if the first set is a superset of the second one.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ('a', 'e')
C = ('a', 'b', 'c', 'd')
print('A is superset of B:', A.issuperset(B)) # Output: A is superset of B: True
print('A is superset of C:', A.issuperset(C)) # Output: A is superset of C: Falseoutput
A is superset of B: True
A is superset of C: FalseExample 4: Check if a Set is superset of a Dictionary
You can also pass iterables like dictionary: issuperset() method first converts the iterables into sets and then verifies if the first set is a superset of the second one.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = {1: 'a', 2: 'e', 'i': 3}
C = {'a': 1, 'e': 2, 'i': 3}
print('A is superset of B:', A.issuperset(B)) # Output: A is superset of B: False
print('A is superset of C:', A.issuperset(C)) # Output: A is superset of C: Trueoutput
A is superset of B: False
A is superset of C: TrueExample 5: Check if a Set is superset of a String
You can also pass iterables like string: issuperset() method first converts the iterables into sets and then verifies if the first set is a superset of the second one.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = 'ae'
C = 'abcd'
print('A is superset of B:', A.issuperset(B)) # Output: A is superset of B: True
print('A is superset of C:', A.issuperset(C)) # Output: A is superset of C: Falseoutput
A is superset of B: True
A is superset of C: FalseExample 6: Check if a Set is superset of Itself
A = {'a', 'e', 'i', 'o', 'u'}
print('A is superset of A:', A.issuperset(A)) # Output: A is superset of A: Trueoutput
A is superset of A: TrueEquivalent Operator >= for issuperset()
Check if a set is a superset of another set can be performed with the >= operator as well, providing an alternative to the issuperset() method.
A = {1, 2, 3, 4, 5}
B = {1, 2, 3}
print(A.issuperset(B)) # Output: True
print(B.issuperset(A)) # Output: False
print(A >= B) # Output: True
print(B >= A) # Output: Falseoutput
True
False
True
FalseFind Proper superset with > operator
To test if the set is a proper superset of other, you can use > comparison operator.
For example:
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 3, 4, 5, 6}
print(A > B) # Output: False
print(B > C) # Output: False
print(B > A) # Output: True
print(C > A) # Output: True
print(C > B) # Output: Trueoutput
False
False
True
True
True