Python Set issuperset() method returns True if all elements of the specified set are present in the original set; otherwise, it returns False.
Visual Representation
Consider this image, and there are two sets, A={1,2,4} and B={1,2,3,4,5}. So we can see that all the elements of set A are present in set B. So here, B is called the Superset of set A. Also, set A is called the Subset of set B.
Syntax
First_Set.issuperset(Second_Set)
Parameters
Second_Set: It checks if First_Set is a superset of Second_Set.
Return Value
It returns a boolean value:
- True: If First_Set is a superset of Second_Set
- False: If First_Set is not a superset of Second_Set
Example: How to Use issuperset()
#Multiples of 2 from 2 to 10
set1 = {2, 4, 6, 8, 10}
# Multiple of 4 from 4 to 10
set2 = {4, 8}
# Multiple of 4 from 4 to 20
set3 = {4, 8, 12, 16, 20}
# Printing sets
print("Set1 is: ", set1)
print("Set2 is : ", set2)
print("Set3 is: ", set3)
# Checking if Set1 is a superset of Set2 and Set3
print("Is Set1 Superset of Set2? ", set1.issuperset(set2))
print("Is Set1 Superset of Set3? ", set1.issuperset(set3))
# Checking if Set2 is a superset of Set3
print("Is Set2 Superset of Set3? ", set2.issuperset(set3))
Output
Set1 is: {2, 4, 6, 8, 10}
Set2 is : {8, 4}
Set3 is: {4, 8, 12, 16, 20}
Is Set1 Superset of Set2? True
Is Set1 Superset of Set3? False
Is Set2 Superset of Set3? False
Using superset (>=) operator
You can also use the superset operator (>=) to check if a set is a superset of another set.
Example
set1 = {2, 4, 6, 8, 10}
set2 = {4, 8}
set3 = {4, 8, 12, 16, 20}
print("Is Set1 Superset of Set2? ", set1 >= set2)
print("Is Set1 Superset of Set3? ", set1 >= set3)
print("Is Set2 Superset of Set3? ", set2 >= set3)
Output
Is Set1 Superset of Set2? True
Is Set1 Superset of Set3? False
Is Set2 Superset of Set3? False