Python Superset Operations

Introduction

While subsets focus on testing whether one set is entirely contained within another, Python superset operations explore the opposite scenario. A set A is considered a superset of set B if all elements of B are present in A. Python provides various ways to determine whether one set is a superset of another, including the issuperset() instance method, the issuperset() class method, the superset operator >= (greater than or equal), and the proper superset operator > (greater than).

Using superset operations is valuable when you want to ensure that a set encompasses all elements of another set. For example, you may want to check whether the inventory of a store includes all the items on a person’s shopping list.

A superset is the same as the subset, except in reverse, where sets and A and B exchange roles. Given this flip, Python subset operations are the same as superset operations. If you’ve read the previous lesson about subsets, this lesson will be very familiar.

Using the issuperset() Instance Method

The issuperset() instance method of Python set objects checks whether all elements of an iterable object, given as an argument, are included in the calling set. It returns True if they are, and False otherwise.

Example:

set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}

result = set_a.issuperset(set_b)
print(result)

Output:

True

In the example above, set_a is a superset of set_b since all elements of set_b are present in set_a, resulting in True.

Example:

set_a = {1, 2, 3}
set_b = {2, 3, 4}

result = set_a.issuperset(set_b)
print(result)

Output:

False

Here, set_a is not a superset of set_b because 4 is in set_b but not in set_a, leading to False.

The next example demonstrates that a set is a superset of itself since this fits the definition of a superset.

Example:

set_a = {1, 2, 3}

result = set_a.issuperset(set_a)
print(result)

Output:

True

The following example demonstrates that issuperset() works with other iterable types, e.g. a list.

Example:

set_a = {1, 2, 3, 4, 5}
lst = [1, 2, 2, 3]

result = set_a.issuperset(lst)
print(result)

Output:

True

In the example above, because the argument given to issubset() is a list and not a set, it can have repeated elements. And regardless of the repeated element, 2, issuperset() works the same way as it does with set arguments. It returns True since all unique elements of lst are found in set_a.

Using the issuperset() Class Method

Similar to the instance method, the issuperset() class method allows you to check whether an iterable, given as the second argument, has all its elements within a set, given as the first.

The difference between the class method and instance method version of issuperset() is that the instance method is called on a specific set object, whereas the class method includes the set object as its first parameter. Following the first argument, which must be a set, the second argument can be objects of any iterable type.

Since this version of issuperset() is a class method of the set class, you call it with a set. prefix.

Example:

set_a = {1, 2, 3, 4, 5}
lst = [1, 2, 2, 3]

result = set.issuperset(set_a, lst)
print(result)

Output:

True

This achieves the same result as the previous example but using the class method.

Using the Superset Operator >=

The >= operator checks whether one iterable object on the left is a superset of another iterable one on the right. It returns True if it is and False otherwise.

Example:

set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}

result = set_a >= set_b
print(result)

Output:

True

In the example above, set_a is a superset of set_b, and therefore the result is True.

Unlike the issuperset() instance and class methods, >= does not require a set object. It will implicitly convert any iterable objects it operates on to sets and treat them as such.

Example:

lst_a = [1, 2, 3, 4, 5]
lst_b = [1, 2, 2, 3]

result = lst_a >= lst_b
print(result)

Output:

True

In the above example, the result is True because lst_a contains all the elements in lst_b at least once. Since both lists are treated as sets by the operator, lst_a does not need to include 2 twice for it to be considered the superset of lst_a.

Using the Proper Superset Operator >

The > operator is the same as >= except that it checks whether one set is a proper superset of another. A proper superset means that all elements of the second set are in the first set, but the first set has at least one more element. In other words, a proper superset is like a regular superset, but identical sets are not allowed.

Example:

set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}

result = set_a > set_b
print(result)

Output:

True

In this example, set_a is a proper superset of set_b because all its elements are in set_b, and it has more elements.

The next example demonstrates how a proper superset differs from a regular superset.

Example:

set_a = {1, 2, 3}

result = set_a > set_a
print(result)

Output:

False

Here, the result of the > operator is False because set_a is not a proper superset of itself.

Summary & Reference for Python Superset Operations

Python superset operations help determine whether one set encompasses all elements of another.


The issuperset() instance method checks whether all elements of an iterable object are present in the calling set. It returns True if they are, and False otherwise.

set_a = {1, 2, 3, 4, 5}
lst = [1, 2, 2, 3, 4]
result = set_a.issuperset(lst)  # result --> True

The issuperset() class method, an alternative to the instance method, is called directly on the set class. Its first argument must be a set, and the second can be an object of any iterable type.

set_a = {1, 2, 3, 4, 5}
lst = [1, 2, 2, 3, 4]
result = set.issuperset(set_a, lst)  # result --> True

The superset operator >= checks whether one iterable is a superset of another. It implicitly converts non-set iterables to sets before performing the comparison.

set_a = [1, 2, 3, 4, 5]
set_b = [1, 2, 2, 3, 4]
result = set_a >= set_b  # result --> True

The proper superset operator > is similar to >= but ensures that the first set is a proper superset of the second, meaning it has more elements.

set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}
result = set_a > set_b  # result --> True

set_a = {1, 2, 3}
set_b = {1, 2, 3}
result = set_a > set_b  # result --> False