Skip to content

Python Set issuperset() Method

How to use Python's issuperset() method to check if one set contains all elements of another set, including support for lists, tuples, dictionaries, and strings.

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

  • True if set my_set is a superset of other_set
  • False if set my_set is not a superset of other_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:

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: 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:

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: 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.

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: 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:

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: False

output

A is superset of B: True
A is superset of C: False

Example 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: 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}

print(A.issuperset(B)) # Output: True
print(B.issuperset(A)) # Output: False

print(A >= B) # Output: True
print(B >= A) # Output: False

output

True
False
True
False

Find 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: True

output

False
False
True
True
True
Navigation