Python - Set issubset() Method


Advertisements

The Python set issubset() method is used to check whether all elements of the set are present in specified set or not. It returns true if all elements of the set are present in specified set, else returns false. Please note that, this method can take iterable as an argument also.

Note: Set B is called subset of set A if all elements of set B are present in set A. The below figure depicts two sets called A = {10, 20, 30, 40, 50, 60} and B = {10, 30, 60}. As all the elements of B are present in A, hence, B is called subset of A.

Subset

Syntax


set.issubset(iterable)

Parameters

iterable Required. specify set or iterable which need to be compared with the given set.

Example:

In the below example, SetA is checked againt SetB, SetC, ListB and ListC for subset using python issubset() method.

SetA = {10, 30, 60}

SetB = {10, 20, 30, 40, 50, 60}
SetC = {5, 10, 15, 20, 25}

ListB = [10, 20, 30, 40, 50, 60]
ListC = [5, 10, 15, 20, 25]

print(SetA.issubset(SetB))
print(SetA.issubset(SetC))

print(SetA.issubset(ListB))
print(SetA.issubset(ListC))

Output

True
False

True
False

Sets Methods




Advertisements