Skip to main content

Python Set issubset() Function

The Set issubset() method returns True if all items in the set are present in the specified set. Otherwise, it returns False.

note

Every set is a subset of itself.

Syntax

my_set.issubset(other_set)

issubset() Parameters

Python Set issubset() function parameters:

ParameterConditionDescription
other_setRequiredA set
note

The issubset() function checks if my_set is a subset of other_set!

note

You can also pass iterables like list, tuple, dictionary or string. In that case, issubset() method first converts the iterables into sets and then verifies if the first set is a subset of the second one.

issubset() Return Value

Python Set issubset() function returns

  • True if set my_set is a subset of other_set
  • False if set my_set is not a subset of other_set

Examples

Example 1: Check if a Set is Subset of Another Set

If set A is subset of B, then issubset() returns True:

A = {'Anna', 'David'}
B = {'Tom', 'David', 'Anna'}

print(A.issubset(B)) # Output: True

output

True

If set A is not subset of B, then issubset() returns False:

A = {'Tom', 'David', 'Anna'}
B = {'Anna', 'David'}

print(A.issubset(B)) # Output: False

output

False

Example 2: Check if a Set is Subset of a List

You can also pass iterables like list: issubset() method first converts the iterables into sets and then verifies if the first set is a subset of the second one.

For example:

# create a Set A
A = {'a', 'e'}

# create List B and C
B = ['a', 'e', 'i', 'o', 'u']
C = ['e', 'i', 'o', 'u']

print('A is subset of B:', A.issubset(B)) # Output: A is subset of B: True
print('A is subset of C:', A.issubset(C)) # Output: A is subset of C: False

output

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

Example 3: Check if a Set is Subset of a Tuple

You can also pass iterables like tuple: issubset() method first converts the iterables into sets and then verifies if the first set is a subset of the second one.

For example:

# create a Set A
A = {'a', 'e'}

# create Tuple B and C
B = ('a', 'e', 'i', 'o', 'u')
C = ('e', 'i', 'o', 'u')

print('A is subset of B:', A.issubset(B)) # Output: A is subset of B: True
print('A is subset of C:', A.issubset(C)) # Output: A is subset of C: False

output

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

Example 4: Check if a Set is Subset of a Dictionary

You can also pass iterables like dictionary: issubset() method first converts the iterables into sets and then verifies if the first set is a subset of the second one.

note

The dictionary keys are used for issubset(), not the values!

For example:

# create a Set A
A = {'a', 'e'}

# create Dictionary B and C
B = {1: 'a', 2: 'e', 'f': 3}
C = {'a': 1, 'e': 2, 'f': 3}

print('A is subset of B:', A.issubset(B)) # Output: A is subset of B: False
print('A is subset of C:', A.issubset(C)) # Output: A is subset of C: True

output

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

Example 5: Check if a Set is Subset of a String

You can also pass iterables like string: issubset() method first converts the iterables into sets and then verifies if the first set is a subset of the second one.

For example:

# create a Set A
A = {'a', 'e'}

# create String B and C
B = 'aeiou'
C = 'aiou'

print('A is subset of B:', A.issubset(B)) # Output: A is subset of B: True
print('A is subset of C:', A.issubset(C)) # Output: A is subset of C: False

output

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

Example 6: Check if a Set is Subset of Itself

# create a Set A
A = {'a', 'e'}

print('A is subset of A:', A.issubset(A)) # Output: A is subset of A: True

output

A is subset of A: True

Equivalent Operator <= for issubset()

Check if a set is a subset of another set can be performed with the <= operator as well, providing an alternative to the issubset() method.

A = {1, 2, 3}
B = {1, 2, 3, 4, 5}

# by issubset() method
print(A.issubset(B)) # Output: True
print(B.issubset(A)) # Output: False

# by <= operator
print(A <= B) # Output: True
print(B <= A) # Output: False

output

True
False
True
False

Find Proper Subset with < operator

note

Set A is considered a proper subset of B, if A is a subset of B, but A is not equal to B.

To test if the set is a proper subset 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}

# by < operator
print(A < B) # Output: True
print(B < C) # Output: False

output

True
False