The issuperset() method checks whether elements from one set are present in another and returns True or False accordingly. For example, Set A is a superset of B if all the elements of Set B are present in Set A.

Python Set issuperset:

Method signature

The signature for the issuperset() method is as shown below. Here, A and B are set. In this case, the issuperset() method returns True if and only if all the elements of set B are present in Set A. Otherwise, it returns False.

A.issuperset(B)

Method parameters and return type

  • The issuperset() method takes one parameter.
  • However, it returns a boolean value True or False depending on the data set.

Python Set issuperset Examples:

Example 1: In this example, let us take a set A and a set B with a set of numbers. Here, result variable stores the boolean value returned by the issuperset method.

#Initializing the Set
A = {14,15,16,19,29,39}
B={19,29,39}
# Checking if A is superset of B
result=A.issuperset(B)
print("Is A superset of B",result)
# Checking if B is superset of A
result=B.issuperset(A)
print("Is B superset of A",result)

Output

Is A superset of B True
Is B superset of A False

Example 2: In this example, let us consider a set containing cities. Just because Python is case sensitive, if the city name doesn’t match exactly, this method will return False. So, an exact match is expected in both sets for the method to return True.

#Initializing the Set
A={"Delhi", "Bhopal", "Chennai", "Jodhpur"}
B={"Delhi", "Chennai"}
C={"delhi", "Bhopal"}
# Checking if A is superset of B and printing
print("Is A superset of B",A.issuperset(B))
# Checking if A is superset of C and printing
print("Is A superset of C",A.issuperset(C))

Output

Is A superset of B True
Is A superset of C False

Example 3: In this example, let us demonstrate the issuperset() method on a mixed set. Here, we are taking a set B representing a record of an employee with his name and id. Thereafter, we have to check the presence of that Employee’s record in a set referred as A that contains data of all employees.

#Initializing the Set
A={"Ram", 15,"Sam", 25, "Harry",3,"John",8}
B={"Sam",25}
# Using issuperset method and printing the result
print("Is A superset of B",A.issuperset(B)) 

Output

Is A superset of B True

Conclusion

The issuperset() method returns a boolean value to indicate if one set is a superset of another or not.

References

Happy Learning 🙂