Python Set issuperset() Function
The Set issuperset()
method returns True
if all items in the specified set are present in the original set. Otherwise, it returns False
.
Every set is a superset of itself.
Syntax
my_set.issuperset(other_set)
issuperset() Parameters
Python Set issuperset()
function parameters:
Parameter | Condition | Description |
---|---|---|
other_set | Required | A set |
The issuperset()
function checks if my_set
is a superset of other_set
!
You can also pass iterables like list, tuple, dictionary or string. In that case, issuperset()
method first converts the iterables into sets and then verifies if the first set is a superset of the second one.
issuperset() Return Value
Python Set issuperset()
function returns
True
if setmy_set
is a superset ofother_set
False
if setmy_set
is 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: True
output
True
If set A is not superset of B, then issuperset()
returns False
:
A = {'Anna', 'David'}
B = {'Tom', 'David', 'Anna'}
print(A.issuperset(B)) # Output: False
output
False
Example 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:
# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}
# create List B and C
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: False
output
A is superset of B: True
A is superset of C: False
Example 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:
# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}
# create Tuple B and C
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: False
output
A is superset of B: True
A is superset of C: False
Example 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.
The dictionary keys are used for issuperset()
, not the values!
For example:
# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}
# create Dictionary B and C
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: True
output
A is superset of B: False
A is superset of C: True
Example 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:
# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}
# create String B and C
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: False
output
A is superset of B: True
A is superset of C: False
Example 6: Check if a Set is superset of Itself
# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}
print('A is superset of A:', A.issuperset(A)) # Output: A is superset of A: True
output
A is superset of A: True
Equivalent 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}
# by issuperset() method
print(A.issuperset(B)) # Output: True
print(B.issuperset(A)) # Output: False
# by >= operator
print(A >= B) # Output: True
print(B >= A) # Output: False
output
True
False
True
False
Find Proper superset with >
operator
Set A
is considered a proper superset of B
, if A
is a superset of B
, but A
is not equal to B
.
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}
# by > operator
print(A > B) # Output: False
print(B > C) # Output: False
print(B > A) # Output: True
print(C > A) # Output: True
print(C > B) # Output: True
output
False
False
True
True
True