Home » Python

Python Set issubset() Method with Example

Python Set issubset() Method: Here, we are going to learn to check whether this set (set1) is the subset of another set (set2) in Python?
Submitted by IncludeHelp, on November 29, 2019

Set issubset() Method

issubset() method 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:

    set1.issubset(set2)

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:

# Python Set issubset() Method with Example

# 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:

# Python Set issubset() Method with Example

# 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.