Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22849

Python for Beginners: Check For Superset in Python

$
0
0

In python, we use sets to store unique immutable objects. In this article,  we will discuss what is a superset of a set. We will also discuss ways to check for superset in Python.

What is a Superset?

A superset of a set is another set that contains all the 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 A is said to be a superset of set B.

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 A is a superset of set B. On the other hand, all the elements of set C do not belong to set A. Hence, set A is not a superset of set C.

You can observe that a superset will always have more or equal elements than the original set. Now, let us describe a step-by-step algorithm to check for a superset  in python.

Suggested Reading: Chat Application in Python

How to Check For Superset in Python?

Consider that we are given two sets A and B. Now, we have to check if set B is a superset of set A or not. For this, we will traverse all the elements of set A and check whether they are present in set B or not. If there exists an element in set A that doesn’t belong to set B, we will say that set B is not a superset of set A. Otherwise, set B will be a superset of set A. 

To implement this approach in Python, we will use a for loop and a flag variable isSuperset. We will initialize the isSuperset variable to True denoting that set B is a superset of set A. Now we will traverse set A using a for loop. While traversing the elements in set A, we will check if the element is present in set B or not. 

If we find any element in A that isn’t present in set B, we will assign False to isSuperset showing that set B is not a superset of the set A. 

If we do not find any element in set A that does not belong to set B, the isSuperset variable will contain the value True showing that set B is a superset of set A. The entire logic to check for superset  can be implemented in Python as follows.

def checkSuperset(set1, set2):
    isSuperset = True
    for element in set2:
        if element not in set1:
            isSuperset = False
            break
    return isSuperset


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 A is superset of B :", checkSuperset(A, B))
print("Set A is superset of C :", checkSuperset(A, C))
print("Set B is superset of C :", checkSuperset(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 A is superset of B : True
Set A is superset of C : False
Set B is superset of C : False

Check For Superset Using issuperset() Method

We can also use the issuperset() method to check for superset in python. The issuperset() method, when invoked on a set A, accepts a set B as input argument and returns True if set A is a superset of B. Otherwise, it returns False.

You can use the issuperset() method to check for superset 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 A is superset of B :", A.issuperset(B))
print("Set A is superset of C :", A.issuperset(C))
print("Set B is superset of C :", B.issuperset(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 A is superset of B : True
Set A is superset of C : False
Set B is superset of C : False

Conclusion

In this article, we have discussed two ways to check for superset 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.

The post Check For Superset in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22849

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>