Python Set issuperset()
Python Set issuperset() method
Python Set issuperset() method is used to check if the set is a superset of another given set.
In this tutorial, you will learn the syntax and usage of Set issuperset() method, with examples.
Syntax of Set issuperset()
The syntax to call issuperset() method is
set.issuperset(s)
Parameters
Set issuperset() method takes one parameter.
Parameter | Description |
---|---|
s | Required Another set object. |
Return value
Set issuperset() method returns a boolean value of True if the set is a superset of given set s, otherwise returns False.
Usage
The following statement shows how to check if a set set_1 is a superset of another set set_2 using set.issuperset() method.
set_1.issuperset(set_2)
Examples
1. Checking if a set is superset of another set in Python
In this example, we shall take two sets in set_1 and set_2. We have to check if the set set_1 is a superset of the set set_2.
Call issuperset() method on set_1 object, and pass set_2 object as argument. Since issuperset() method returns a boolean value, we can use the call to the method issuperset() as a condition in Python if else statement.
Python Program
# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'c'}
if set_1.issuperset(set_2):
print("set_1 is SUPERSET of set_2.")
else:
print("set_1 is NOT SUPERSET of set_2.")
Output
set_1 is SUPERSET of set_2.
Since set_1 contains all the items of set_2, set_1 is a superset of set_2.
set_1.issuperset(set_2) returns True, and the if-block is executed.
Now, let us take the items in these two sets, such that set_1 is not a superset of set_2, and observe the output.
Python Program
# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'k', 'c'}
if set_1.issuperset(set_2):
print("set_1 is SUPERSET of set_2.")
else:
print("set_1 is NOT SUPERSET of set_2.")
Output
set_1 is NOT SUPERSET of set_2.
Since set_1 does not have all the items of set_2, set_1 is not a superset of set_2.
set_1.issuperset(set_2) returns False, and the else-block is executed.
Summary
In this Python Set Tutorial, we learned how to use Set issuperset() method to check if a set is a superset of another set in Python, with the help of well detailed examples.