Open In App

Python Set issubset() Method

Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python.

Python Set issubset() Method Syntax

Syntax: set_obj.issubset(other_set)



Parameter:

  • other_set: any other set to compare with.

Return: bool



Python set issubset() Method Example

This code checks if set s2 is a subset of set s1 and prints True if it is.




s1 = {1, 2, 3, 4, 5}
s2 = {4, 5}
print(s2.issubset(s1))

Output:

True

Python set issubset() method

How Python set issubset() Work

In this code, it checks whether set A is a subset of set B and then whether set B is a subset of set A. The first print statement returns True because all elements of set A are also in set B. The second print statement returns False because set B contains elements that are not in set A.




A = {4, 1, 3, 5}
B = {6, 0, 4, 1, 5, 0, 3, 5}
 
print(A.issubset(B))
print(B.issubset(A))

Output

True
False

Working with three-set using issubset()

In this code, It checks if one set is a subset of another set using the .issubset() method.




A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}
 
print(A.issubset(B))
print(B.issubset(A))
 
 
print(A.issubset(C))
print(C.issubset(B))

Output

True
False
False
True

Article Tags :