Beginner Level
Intermediate Level
Advanced Level
Introduction
Subsets are an essential part of set theory and have vast applications in computing and data science. In Python, subsets are essentially a set of smaller collections of elements derived from a larger collection, where each element in the subset is selected based on a certain criterion. You might need to extract specific subsets of data from large datasets, or maybe you are simply curious about how subsets work in Python.
Table of Contents :
- What is a Subset
- using issubset() method
- using the subset operator
- using the proper subset operator
What is a Subset :
- If all the elements of
set_1
are also present inset_2
thenset_1
is called a subset ofset_2
. - If
set_1
is not equal toset_2
thenset_1
is called a proper subset ofset_2
. - A set is also a subset of itself.
- In Python we can find if one set is a subset of another by using the :
- issubset() method
- subset operator '<='
- proper subset operator '<'
using issubset() method :
- This built-in method
issubset()
can be used to check if a set is a subset of another or not. - The basic syntax of using the
issubset()
method is :bool_value = set_1.issubset(set_2)
- The
issubset()
method returns a boolean value :- True
set_1
is a subset ofset_2
- False if
set_1
is not a subset ofset_2
- True
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_1.issubset(set_2)
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_1 is subset of set_2 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_1 is subset of set_2 = True
using the subset operator '<=' :
- The subset operator can be used to check if a set is a subset of another or not.
- The basic syntax of using subset operator is :
set_1 <= set_2
- The subset operator returns a boolean value :
- True if
set_1
is a subset ofset_2
- False if
set_1
is not a subset ofset_2
- True if
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_1 <= set_2
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_1 is subset of set_2 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_1 is subset of set_2 = True
using the proper subset operator '<' :
- The proper subset operator can be used to check if a set is a proper subset of another or not.
- The basic syntax of using proper subset operator is :
set_1 < set_2
- The proper subset operator returns a boolean value :
- True if
set_1
is a proper subset ofset_2
- False if
set_1
is not a proper subset ofset_2
- True if
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_1 < set_2
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_1 is proper subset of set_2 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_1 is proper subset of set_2 = True
Prev. Tutorial : Symmetric difference on sets
Next Tutorial : Supersets