Python Set issubset()

Python Set x.issubset(y) method checks if the set x is a subset of y. The method returns True if all the elements of set x are present in set y, else it returns False.

In this tutorial, we will learn the syntax of set.issubset() method and go through examples covering different scenarios for the arguments that we pass to issubset() method.

Syntax

The syntax of set.issubset() method is

</>
Copy
set.issubset(other)

where

ParameterDescription
otherA set.

Examples

1. Check if set x is subset of y

In the following program, we will take two sets: x, y; such that all the elements of x are present in y. x.issubset(y) must return True.

Python Program

</>
Copy
x = {'apple', 'banana', 'cherry'}
y = {'apple', 'banana', 'cherry', 'mango', 'guava'}

if x.issubset(y) :
    print('x is subset of y.')
else :
    print('x is not subset of y.')

Program Output

x is subset of y.

2. Set x is not subset of set y

In the following program, we will take two sets: x, y; such that all of the elements of x are not present in y. x.issubset(y) must return False.

Python Program

</>
Copy
x = {'apple', 'banana', 'cherry'}
y = {'apple', 'banana', 'mango', 'guava'}

if x.issubset(y) :
    print('x is subset of y.')
else :
    print('x is not subset of y.')

Program Output

x is not subset of y.

Conclusion

In this Python Tutorial, we learned about Python set method issubset(). We have gone through the syntax of issubset() method, and its usage with example programs.