In this tutorial, we will look at how to check whether a set in Python is a superset of another set with the help of some examples.
What is a superset?
Let’s say we have two sets, A and B. Now, if all the elements of set A are present in set B then A is said to be a subset of B, and B is called the superset of A. Here’s an example –
You can see that set B contains all the elements of set A and thus B is a superset of A and A is a subset of B.
Check if a set is a superset of another set in Python
The Python set data structure comes with a number of built-in functions to accomplish common set operations like union, intersection, difference, etc. You can use the Python set issuperset()
function to check whether a set is a superset of another set. The following is the syntax:
# check if a is a superset of b a.issuperset(b)
We call the issuperset()
function from set a and pass the set b as an argument to check whether set a is a superset of set b. It returns a boolean value. Let’s look at an example.
# create two sets a = {1, 2, 3, 4} b = {1, 2, 3} # check if a is superset of b a.issuperset(b)
Output:
True
We get True
as the output since a contains all the elements from set b and hence is a superset of set b. Let’s look at another example.
# create two sets a = {1, 2} b = {1, 2, 3} # check if a is superset of b a.issuperset(b)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
False
Here we get False
as the output since a does not contain all the elements from set b and hence it’s not a superset of set b.
Alternatively, you can use the >=
operator to check if a set is a superset of another set. For example,
# create two sets a = {1, 2, 3, 4} b = {1, 2, 3} # check if a is superset of b a >= b
Output:
True
We get the same result as we did with the set issuperset()
function.
You might also be interested in –