Python Set issubset() Method (with Examples)

Python Set issubset() Method: In this tutorial, we will learn about the issubset() method of the set class with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 14, 2023

Python Set issubset() Method

The issubset() is an inbuilt method of the set class that is used to check whether this set (set1) is the subset of the set2, the method called with set1 and set2 is supplied as an argument, this method returns "True" is all elements of set1 present in the set2, else the method returns "False".

Syntax

The following is the syntax of issubset() method:

set1.issubset(set2)

Parameter(s):

The following are the parameter(s):

  • set1 – It represents the set1 (this set).
  • set2 – It represents the set2 (another set to be compared).

Return Value

The return type of this method is <class 'bool'>, it returns a Boolean value (True or False).

Example 1: Use of Set issubset() Method

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
cars_3 = {"Porsche", "Audi", "Lexus", "Mazda", "Lincoln"}

# issubset() method call
result = cars_1.issubset(cars_2)
print("cars_1.issubset(cars_2): ", result)

result = cars_2.issubset(cars_3)
print("cars_2.issubset(cars_3): ", result)

result = cars_1.issubset(cars_3)
print("cars_1.issubset(cars_3): ", result)

# checking using condition
if cars_1.issubset(cars_2):
    print("cars_1 is subest of cars_2")
else:
    print("cars_1 is not subest of cars_2")

if cars_2.issubset(cars_3):
    print("cars_2 is subest of cars_3")
else:
    print("cars_2 is not subest of cars_3")

if cars_1.issubset(cars_3):
    print("cars_1 is subest of cars_3")
else:
    print("cars_1 is not subest of cars_3")

Output

cars_1.issubset(cars_2):  False
cars_2.issubset(cars_3):  True
cars_1.issubset(cars_3):  True
cars_1 is not subest of cars_2
cars_2 is subest of cars_3
cars_1 is subest of cars_3

Example 2: Use of Set issubset() Method

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "UVW"}

# issubset() method calls
result = x.issubset(y)
print("x.issubset(y): ", result)

result = y.issubset(z)
print("y.issubset(z): ", result)

result = z.issubset(x)
print("z.issubset(x): ", result)

result = x.issubset(z)
print("x.issubset(z): ", result)

result = y.issubset(x)
print("y.issubset(x): ", result)

Output

x.issubset(y):  True
y.issubset(z):  False
z.issubset(x):  False
x.issubset(z):  False
y.issubset(x):  True


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.