The Python issuperset() function allows you to check if a set is a superset of another set.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(b.issuperset(a))

#Output:
True

When working with different collections of data, the ability to easily determine properties of these objects can be useful.

One such property is if a set is a superset of another set.

A set X is a superset of another set Y if all of the elements of the set Y are in the set X.

In Python, you can use the issuperset() function to check if a set is a superset of another set. issuperset() returns a boolean value.

Below is a simple example showing you how to check if a set is a superset of another set using the issuperset() function in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(b.issuperset(a))

#Output:
True

Using > and ≥ Operators to Check if Set is Superset in Python

In addition to the issuperset() function, you can also use the > and ≥ operators to check if a set is a superset of another set.

The > operator checks if the superset is a proper superset, and ≥ checks if the set is a superset with the chance of it being equal.

Below are some examples showing how to use the > and ≥ operators to check if a set is a superset of another set in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(b > a)
print(b > b)
print(b >= a)
print(b >= b)

#Output:
True
False
True
True

Using Python issubset() Function to Check if Set is Superset of Another Set

If you want to go the other way and check if a set is a subset of another set, or that all of the elements of the set are also in the other set, then you can use the issubset() function.

Just like issuperset(), issubset() returns a boolean value.

Below is a simple example showing you how to check if a set is a superset of another set using the issubset() function in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(a.issubset(b))

#Output:
True

Hopefully this article has been useful for you to learn how to use the issuperset() function in Python.

Categorized in:

Python,

Last Update: March 12, 2024