• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check For Superset in Python

Check For Superset in Python

Author: Aditya Raj
Last Updated: August 8, 2022

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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us