Python Set – issubset() Method

It is a method in python that checks if the given set is a subset of the other or not. Returns True if the given set is present in the other set (passed in the argument) else returns False. It doesn’t modifies the given set.

Syntax:
set1.issubset(set2)
or
set1<=(set2)

Also Read: Python Set – len() Function

Example:

# Python code to understand issubset() method in set
# www.codewindow.in

s1 = {2, 5, 8, 7, 9}
s = {2, 5, 8}
s2 = {2, 8, 9}
s3 = {7, 5}

#is s a subset of s1?
print(s.issubset(s1))

#is s1 a subset of s?
print(s1.issubset(s))

#is s3 a subset of s1?
print(s3<=(s1))

#is s2 a subset of s1?
print(s2.issubset(s1))

Output:

True
False
True
True

Explanation:
Here, all the elements of s are present in set s1.
All the elements of s1 are not present in s.
All the elements of s3 are present in set s1.
All the elements of s2 are present in set s1.


Follow Us

You Missed

Also Checkout