Prerequisite:
Python set() Function with Examples
Set issuperset() Method in Python:
If all items in the specified set exist in the original set, the issuperset() method returns true; otherwise, it returns False.
For Example,
Let p = {1, 2, 3}
q= {1, 2, 3, 4, 5, 6}
Here all the elements of set ‘p ‘ are in the set ‘q’ . Hence set q is the superset of set p.
And set p is the subset of set q.
Syntax:
set.issuperset(set)
Parameters
set: This is Required. It is the set to look for items that are similar.
Return Value:
issuperset() gives
- If A is a superset of B, then this statement is true.
- If A is not a superset of B, the statement is false.
Examples:
Example1:
Input:
Given first set = {20, 30, 100} Given second set = {100, 20, 80, 70, 30}
Output:
Check if the given second set is superset of the first set: True
Example2:
Input:
Given first set = {20, 30, 100, 12, 15, 16} Given second set = {12, 15, 16}
Output:
Check if the given second set is superset of the first set: False
Explanation:
Here the first set is the superset of the second set. But Not the second set is a superset of the first. Hence it returns False.
Set issuperset() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first set as static input and store it in a variable.
- Give the second set as static input and store it in another variable.
- Apply the issuperset() method to the given first and second sets to check if the given second set is the superset of the first set or not.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Give the first set as static input and store it in a variable. fst_set = {20, 30, 100} # Give the second set as static input and store it in another variable. scnd_set = {100, 20, 80, 70, 30} # Apply the issuperset() method to the given first and second sets to check if the # given second set is the superset of the first set or not. # Store it in another variable. rslt = scnd_set.issuperset(fst_set) # Print the above result. print("Check if the given second set is superset of the first set:", rslt)
Output:
Check if the given second set is superset of the first set: True
Similarly, check for the other example
fst_set = {20, 30, 100, 12, 15, 16} scnd_set = {12, 15, 16} rslt = scnd_set.issuperset(fst_set) print("Check if the given second set is superset of the first set:", rslt)
Output:
Check if the given second set is superset of the first set: False
Explanation:
Here the first set is the superset of the second set. But Not the second set is a superset of the first. Hence it returns False.
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first set as user input using set(),map(),input(),and split() functions.
- Store it in a variable.
- Give the second set as user input using set(),map(),input(),and split() functions.
- Store it in another variable.
- Apply the issuperset() method to the given first and second sets to check if the given second set is the superset of the first set or not.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Give the first set as user input using set(),map(),input(),and split() functions. # Store it in a variable. fst_set = set(map(int, input( 'Enter some random Set Elements separated by spaces = ').split())) # Give the second set as user input using set(),map(),input(),and split() functions. # Store it in another variable. scnd_set = set(map(int, input( 'Enter some random Set Elements separated by spaces = ').split())) # Apply the issuperset() method to the given first and second sets to check if the # given second set is the superset of the first set or not. # Store it in another variable. rslt = scnd_set.issuperset(fst_set) # Print the above result. print("Check if the given second set is superset of the first set:", rslt)
Output:
Enter some random Set Elements separated by spaces = 10 12 14 16 Enter some random Set Elements separated by spaces = 12 13 Check if the given second set is superset of the first set: False