Python Set issuperset() Method (with Examples)

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

Python Set issuperset() Method

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

Syntax

The following is the syntax of issuperset() method:

set1.issuperset(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 issuperset() Method

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

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

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

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

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

if cars_2.issuperset(cars_3):
    print("cars_2 is superset of cars_3")
else:
    print("cars_2 is not superset of cars_3")

if cars_1.issuperset(cars_3):
    print("cars_1 is superset of cars_3")
else:
    print("cars_1 is not superset of cars_3")

Output

cars_1.issuperset(cars_2):  True
cars_2.issuperset(cars_3):  False
cars_1.issuperset(cars_3):  True
cars_1 is superset of cars_2
cars_2 is not superset of cars_3
cars_1 is superset of cars_3

Example 2: Use of Set issuperset() Method

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

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

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

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

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

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

Output

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



Comments and Discussions!

Load comments ↻






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