Home »
Python
Python Set issuperset() Method with Example
Python Set issuperset() Method: Here, we are going to learn to check whether this set (set1) is the superset of another set (set2) in Python?
Submitted by IncludeHelp, on November 29, 2019
Set issuperset() Method
issuperset() method 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:
set1.issuperset(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 issuperset() Method with Example
# 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:
# Python Set issuperset() Method with Example
# 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
ADVERTISEMENT
ADVERTISEMENT