How to check if a set is a subset of another set in python ?

Published: December 19, 2020

Tags: Python;

DMCA.com Protection Status

To check if a set is a subset of another set in python, a solution is to use the python function issubset:

Lets create a first set called s:

s = {1,2,3,4,5,6,7,8,9}

to check if another subset called s1 defined as

s1 = {1}

is a subset of set s just called issubset():

print( s1.issubset(s) )

returns here

True

Another example with a set of 2 elements:

s1 = {4,6}

print( s1.issubset(s) )

gives

True

or 4 elements:

s1 = {4,6,7,1}

print( s1.issubset(s) )

gives

True

but

s1 = {7,12}

print( s1.issubset(s) )

returns

False