A set in python is a data structure that contains unique immutable objects. In this article, we will discuss what is a subset of a set and how we can check for subset in python.
What is a Subset?
A subset of a set is another set that contains some or all elements of the given set. In other words, If we have a set A and set B, and each element of set B belongs to set A, then set B is said to be a subset of set A.
Let us consider an example where we are given three sets A, B, and C as follows.
A={1,2,3,4,5,6,,7,8}
B={2,4,6,8}
C={0,1,2,3,4}
Here, you can observe that all the elements in set B are present in set A. Hence, set B is a subset of set A. On the other hand, all the elements of set C do not belong to set A. Hence, set C is not a subset of set A.
You can observe that a subset will always have fewer or equal elements than the original set. An empty set is also considered a subset of any given set. Now, let us describe a step-by-step algorithm to check for a subset in python.
How to Check For Subset in Python?
Consider that we are given two sets A and B. Now, we have to check if set B is a subset of set A or not. For this, we will traverse all the elements of set B and check whether they are present in set A or not. If there exists an element in set B that doesn’t belong to set A, we will say that set B is not a subset of set A. Otherwise, set B will be a subset of set A.
To implement this approach in Python, we will use a for loop and a flag variable isSubset
. We will initialize the isSubset
variable to True denoting that set B is a subset of set A. We have done this to make sure that an empty set B is also considered a subset of A. While traversing the elements in set B, we will check if the element is present in set A or not.
If we find any element that isn’t present in set A, we will assign False
to isSubset
showing that set B is not a subset of the set A.
If we do not find any element in set B that does not belong to set A, the isSubset
variable will contain the value True
showing that set B is a subset of set A. The entire logic to check for subset can be implemented in Python as follows.
def checkSubset(set1, set2):
isSubset = True
for element in set1:
if element not in set2:
isSubset = False
break
return isSubset
A = {1, 2, 3, 4, 5, 6, 7, 8}
B = {2, 4, 6, 8}
C = {0, 1, 2, 3, 4}
print("Set {} is: {}".format("A", A))
print("Set {} is: {}".format("B", B))
print("Set {} is: {}".format("C", C))
print("Set B is subset of A :", checkSubset(B, A))
print("Set C is subset of A :", checkSubset(C, A))
print("Set B is subset of C :", checkSubset(B, C))
Output:
Set A is: {1, 2, 3, 4, 5, 6, 7, 8}
Set B is: {8, 2, 4, 6}
Set C is: {0, 1, 2, 3, 4}
Set B is subset of A : True
Set C is subset of A : False
Set B is subset of C : False
Suggested Reading: Chat Application in Python
Check For Subset Using issubset() Method
We can also use the issubset()
method to check for subset in python. The issubset()
method, when invoked on a set A, accepts a set B as input argument and returns True
if set A is a subset of B. Otherwise, it returns False
.
You can use the issubset()
method to check for subset in python as follows.
A = {1, 2, 3, 4, 5, 6, 7, 8}
B = {2, 4, 6, 8}
C = {0, 1, 2, 3, 4}
print("Set {} is: {}".format("A", A))
print("Set {} is: {}".format("B", B))
print("Set {} is: {}".format("C", C))
print("Set B is subset of A :", B.issubset(A))
print("Set C is subset of A :", C.issubset(A))
print("Set B is subset of C :", B.issubset(C))
Output:
Set A is: {1, 2, 3, 4, 5, 6, 7, 8}
Set B is: {8, 2, 4, 6}
Set C is: {0, 1, 2, 3, 4}
Set B is subset of A : True
Set C is subset of A : False
Set B is subset of C : False
Conclusion
In this article, we have discussed ways to check for subset in python. To learn more about sets, you can read this article on set comprehension in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.