Subset and Superset are two fundamental concepts in set theory. Python also has built-in methods to support these concepts.
Supersets
A set is defined as the superset of another set, if second set all elements are also elements of first set. i.e. set A
will be the superset of set B
if all elements of set B
are also elements of set A
. In other words superset can be defined as inverse of subset.
Superset – Mathematical Definition
Superset is denoted with symbol ⊇
. Above definition can be denoted as A ⊇ B
. If set A
is a superset of set B
, then it means that set A
and set B
are either equal OR all elements of set B
are also elements of set A
.
In Mathematics, subset can be defined as A ⊇ B ⟺ ∀ x ( x ∈ B ⟹ x ∈ A )
. Lets understand this with an example:
A = {10, 20, 30} and B = {10, 20}
Here, A ⊇ B
because both elements of set B (10 and 20) are also elements of set A.
- Every set is a superset for empty set. If we have any set B,
B ⊇ ∅
.
Set A superset of Set B
=> A ⊇ B
Superset symbol ⊇
also includes equality in it. This leads to the concept of Proper Superset.
Proper Superset: set A
is a proper superset of set B
, if A ⊇ B
and A ≠ B
. This is denoted as A ⊃ B
. Example – A = {10, 20, 30}
and B = {10, 20}
. Here, A ⊃ B
because set B is contained within set A and A ≠ B
.
Properties of Supersets
- Reflexivity: Every set is always a superset of itself,
A ⊇ A
. - Transitivity: If
A ⊇ B
andB ⊇ C
, thenA ⊇ C
. It means, if set A is a superset of set B and set B is a superset of set C, then it make set A superset of Set C also. Example –A = {10, 20, 30, 40}, B = {10, 20, 30}, C = {10, 20}
- Antisymmetry: If
A ⊇ B
andB ⊇ A
, thenA = B
. It means, if set A is a superset of set B and set B is a superset of set A, then both set A and set B should be equal. Example –A = {10, 20}, B = {10, 20}
Python provides built-in method issuperset()
to check if the set is superset to another set or not. Lets see, how issuperset()
method works in Python.
issuperset()
method
Using - Syntax –
set_1.issuperset(set_2)
issuperset()
method takes a single “iterable” as an argument.set_1.issuperset(set_2)
method returns boolean (True
orFalse
) value.True
meansset_1
is a superset ofset_2
andFalse
meansset_1
is not a superset ofset_2
.
# using issuperset() method
print("using issuperset() method")
set_1 = {10, 20, 30, 70, 80, 90}
set_2 = {70, 80, 90}
superset_set12 = set_1.issuperset(set_2) # check set_1 is superset of set_2
superset_set21 = set_2.issuperset(set_1) # check set_2 is superset of set_1
print(set_1)
print(set_2)
print(superset_set12) # will return True = set_1 is superset of set_2
print(superset_set21) # will return False = set_2 is not superset of set_1
We have defined two sets referenced by variables set_1
and set_2
. Using built-in issuperset()
method we are checking if set_1
and set_2
are superset to each other or not. Output from issuperset()
method will be a boolean value.
Program Output
using issuperset() method
{80, 20, 90, 70, 10, 30} # elements of set_1
{80, 90, 70} # elements of set_2
True # set_1 is superset of set_2
False # set_2 is superset of set_1
From the program output, set_1
and set_2
elements are printed. issuperset()
method has return a boolean (True
or False
) value. True
for set_1
is superset of set_2
and False
when set_2
is not superset of set_1
.
issuperset()
method – with empty sets
Using - Syntax –
set_1.issuperset(set_2)
# using issuperset() method - with empty set
print("using issuperset() method - with empty set")
set_1 = set()
set_2 = set()
superset_set12 = set_1.issuperset(set_2) # check set_1 is superset of set_2
superset_set21 = set_2.issuperset(set_1) # check set_2 is superset of set_1
print(set_1)
print(set_2)
print(superset_set12) # will return True = set_1 is superset of set_2
print(superset_set21) # will return True = set_2 is superset of set_1
We have defined two sets referenced by variables set_1
and set_2
. Both sets are empty sets. Using built-in issuperset()
method we are checking if set_1
is superset of set_2
OR set_2
is superset of set_1
. Output from issuperset()
method will be a boolean value.
Program Output
using issuperset() method - with empty set
set() # elements of set_1
set() # elements of set_2
True # set_1 is superset of set_2
True # set_2 is superset of set_1
Since both set_1
and set_2
are empty sets, so they are superset to each other. As an output we got True for both set_1
superset of set_2
and set_2
superset of set_1
.
>=
) operator
Using superset (- Syntax –
set_1 >= set_2
set_1 >= set_2
returns boolean (True
orFalse
) value.True
meansset_1
is a superset ofset_2
andFalse
meansset_1
is not a superset ofset_2
.
# using superset (>=) operator
print("using superset (>=) operator")
set_1 = {10, 20, 30}
set_2 = {10, 20, 30, 70, 80, 90}
superset_set12 = set_1 >= set_2 # check set_1 is superset of set_2
superset_set21 = set_2 >= set_1 # check set_2 is superset of set_1
print(set_1)
print(set_2)
print(superset_set12) # will return False = set_1 is not superset of set_2
print(superset_set21) # will return True = set_2 is superset of set_1
We have defined two sets referenced by variables set_1
and set_2
. Using superset (>=
) operator, we are checking if set_1
and set_2
are superset to each other or not. Output from superset >=
) operator will be a boolean value.
Program Output
using superset (>=) operator
{10, 20, 30} # elements of set_1
{80, 20, 90, 70, 10, 30} # elements of set_2
False # set_1 is not superset of set_2
True # set_2 is superset of set_1
From the program output, set_1
and set_2
elements are printed. superset (>=
) operator has return a boolean (True
or False
) value. False
shows that set_1
is not superset of set_2
and True
shows that set_2
is superset of set_1
.
>
) operator
Using proper (strict) superset (- Syntax –
set_1 > set_2
(Note – we have only greater than operator, equal to sign is not with it) set_1 > set_2
returns boolean (True
orFalse
) value.True
meansset_1
is a superset ofset_2
andFalse
meansset_1
is not a superset ofset_2
.- This operator is useful to check if set_1 is proper superset of set_2 or not.
# using strict superset (>) operator
print("using strict superset (>) operator")
set_1 = {10, 20, 30}
set_2 = {10, 20, 30, 70, 80, 90}
superset_set12 = set_1 > set_2 # check set_1 is proper superset of set_2
superset_set21 = set_2 > set_1 # check set_2 is proper superset of set_1
print(set_1)
print(set_2)
print(superset_set12) # will return False = set_1 is not superset of set_2
print(superset_set21) # will return True = set_2 is superset of set_1
We have defined two sets referenced by variables set_1
and set_2
. Using proper (strict) superset (>
) operator, we are checking if set_1
and set_2
are proper superset to each other or not. Output from proper (strict) superset (>
) operator will be a boolean value.
Program Output
using strict superset (>) operator
{10, 20, 30} # elements of set_1
{80, 20, 90, 70, 10, 30} # elements of set_2
False # set_1 is not superset of set_2
True # set_2 is superset of set_1
From the program output, set_1
and set_2
elements are printed. Proper (strict) superset (>
) operator has return a boolean (True
or False
) value. False
shows that set_1
is not proper superset of set_2
and True
shows that set_2
is proper superset of set_1
.
issuperset
() method
Custom implementation – Internally in Python, issuperset()
method checks for all elements of set_2
are present in set_1
. If any element of set_2
is not present in set_1
, then method returns False
; otherwise, it returns True
.
Lets see the custom code to implement functioning of issuperset()
method.
# Identifies if set_1 is superset of set_2
def custom_issuperset(set_1, set_2):
# Iterate through elements of set_2
for element in set_2:
# Check if any element of set_2 not in set_1
if element not in set_1:
return False
return True
set_1 = {10, 20, 30}
set_2 = {10, 20, 30, 40, 50, 60}
set_3 = set()
set_4 = set()
print(custom_issuperset(set_1, set_2)) # Output: False
print(custom_issuperset(set_2, set_1)) # Output: True
print(custom_issuperset(set_3, set_4)) # Output: True
custom_issuperset()
function iterates through each element inset_2
.- For each element in
set_2
, it checks if the element is present inset_1
or not. - If any element is not found in
set_1
, the function returnsFalse
, else it will continue to check for all the elements ofset_2
. If all elements ofset_2
are present inset_1
, then it will returnTrue
.
Summary
In this article we learn about various subset operation on Sets. Following scenarios were explored:
- Superset – Mathematical Definition
- Using issuperset() method
- Using issuperset() method – with empty sets
- Using superset (>=) operator
- Using proper (strict) superset (>) operator
- Custom implementation – issuperset() method
Code – Github Repository
All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.
Python Topics
Interview Questions & Answers
issuperset()
be used with other iterables like lists and dictionary?
Q: Can Yes, the issuperset()
method can be used with any iterable e.g., lists, tuples, etc. The method checks whether all elements of the iterable are present in the set from which issuperset()
is called.
- With lists, it checks if all elements of the list are present in the set.
- With dictionaries it checks if all dictionary keys are present in the set.
set_1 = {10, 20, 30, 40}
list_1 = [10, 20, 30]
print(set_1.issuperset(list_1)) # True
dict_1 = {10: 'Python', 20: 'Power BI', 30: 'LLM'}
print(set_1.issuperset(dict_1)) # True
issuperset()
method?
Q: What is the time complexity of the Time complexity of the issuperset()
method is O(n), where n
is the number of elements in the set provided as an argument. The method checks for the presence of each element of the provided set in the original set.
issuperset()
be used to compare sets with different data types?
Q: Can Yes, issuperset()
can be used to compare sets that contain different data types. The method checks for the presence of elements, irrespective of their data types.
issuperset()
is not iterable?
Q: What happens if the argument passed to If a non-iterable object (like an integer) is passed to the issuperset()
method, a TypeError
will be raised. This is because the method expects an iterable to check the elements against the set.
set_1 = {10, 20, 30, 40}
try:
print(set_1.issuperset(10))
except TypeError as e:
print(e) # 'int' object is not iterable