Python Set issubset()

The issubset() method returns True if set A is the subset of B, i.e. if all the elements of set A are present in set B . Else, it returns False.

Example

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

# all items of A are present in B print(A.issubset(B))
# Output: True

issubset() Syntax

The syntax of the issubset() method is:

A.issubset(B)

Here, A and B are two sets.


issubset() Parameter

The issubset() method takes a single argument:

  • B - a set that is a superset of A, which means B has all the items of set A.

issubset() Return Value

The issubset() method returns:

  • True- if set A is a subset of B
  • False- if set A is not a subset of B

Example: Python Set issubset()

A = {'a', 'c', 'e'}
B = {'a', 'b', 'c', 'd', 'e'}

print('A is subset of B:', A.issubset(B))
print('B is subset of A:', B.issubset(A))

Output

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

In the above example, we have used the issubset() method to check if sets A and B are subsets of each other.

Since all elements of A are present in B, the issubset(B) method returns True. On the other hand, set B is not a subset of A. Thus, we get the False with issubset(A).


Recommended Reading: Python Set issuperset()

Did you find this article helpful?