Skip to content

Python Set issubset() Method

How to use Python's issubset() method to check if all elements of one set are contained in another, including compatibility with lists, tuples, dictionaries, and strings.

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

Syntax

my_set.issubset(other_set)

issubset() Parameters

Python Set issubset() method parameters:

Parameter Condition Description
other_set Required A set

issubset() Return Value

Python Set issubset() method 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:

A = {'a', 'e'}

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:

A = {'a', 'e'}

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.

For example:

A = {'a', 'e'}

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:

A = {'a', 'e'}

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

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}

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

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

output

True
False
True
False

Find Proper Subset with < operator

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}

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

output

True
False
Navigation