Python Set issubset
Now, we are going to learn about the issubset method. As the name suggests, with this method, we can check if one set is a subset of another set. Well, if you are familiar with the concept of a subset, it would be easy for you to understand the concept, but even if you are not familiar with the concept, do not worry, since we are going to consider the concept from the basics, so you can follow along.
Let’s consider that there are two sets set1, and set2. We say that set1 is a subset of set2 if all the elements in set1 are there in set2. For example, if set1 = {1, 2, 3, 4, 5} and set2 = {2, 3, 4}, and if we say that check whether the set2 is a subset of set1, then the simple answer would be yes since all the elements from the set2 are there in the set1.
Python Set issubset() Method
Let’s have a look at a simple program, which demonstrates to us the issubset method.
As you can see in the above program, we have two sets, set1, and set2. Also, after that, we are checking if set2 is the subset of set1(we write it as set2.issubset(set1)). The simple answer is True since all the elements from set2 are there in set1. But here, if we check if set1 is the subset of set2, then it is False since all the elements in set1 are not there in set2. However, we can say that set1 is the superset of set2.
Have a look at the below program, which demonstrates the same thing, that set1 is not the subset of set2.
As you can see in the above program, we have two sets, and then we are trying to check if set1 is a subset of set2, which is not true since all the elements from set1 are not there in set2.
So, if we want to check if some set is a subset of another set, we can simply make use of the issubset method. Remember that we can say that set1 is the subset of set2 if all the elements from set1 are there in set2.