Python Set issuperset() Method with examples

Python Set issuperset

Now, we are going to learn about the issuperset method. If you are already familiar with what is a superset, then it would be just easy for you, to get the concept, but even if you are new to the concept, do not worry, since we would consider the concept from the basics, so you can follow along.

Let’s consider that there are two sets, set1, and set2. Now, we say that set1 is the superset of set2, if all the elements from set2, are there in set1. For example, if set1 = {1, 2, 3, 4, 5} and set2 = {1, 2, 3}, then since the set1 contains all the elements from the set2, the set1 is the superset of set2.

Python Set issuperset() Method

Let’s have a look at a simple program, which demonstrates the same thing here.

As you can see in the above program, we have two sets, set1, and set2. Then we are trying to check if set1 is the superset of set2. And the answer that we are going to get is going to be True, or False. It is true in our case, since all the elements that are in the set2, are there in the set1, so the set1 is the superset of the set2. So, if you try to execute the above program and observe the output, you can simply find that the output comes out to be True.

Again, Let’s consider one more example, where set1 is not a superset of set2. This is the condition, when all the elements from set2, are not there in set1. Let’s see the sets, and you would get the concept soon.

As you can see in the above program, we have two sets again, and this time, if we observe the two sets, then we can find that the set1 is not the superset of set2, since all the elements from the set2, are not there in the set1. So, if you try to execute the above program, you can simply find that the output is False.

So, this way, whenever we need to find whether or not, some set is a superset of another set, we can make use of the issuperset method. Remember that this method is going to return True or False, based on whether the set is a superset of another set or not. You can store the returned value somewhere, but since we were more interested in the output, we printed it directly.