Python Set issubset()

Python’s set.issubset(set) returns True if all elements of this set are members of the specified set argument. It determines whether the set on which the method is called is the subset of the specified set.

Here’s a minimal example that checks whether sets s is a subset of t:

>>> s = {'Alice', 'Bob'}
>>> t = {'Alice', 'Bob', 'Carl', 'Liz'}
>>> s.issubset(t)
True

Another minimal Harry Potter example:

Python Set issubset() example
>>> hogwarts = {'Ron', 'Harry', 'Hermione', 'Dumbledore', 'Parvati', 'Malfoy'}
>>> gryffindors = {'Ron', 'Harry', 'Hermione'}
>>> gryffindors.issubset(hogwarts)
True

Syntax

Let’s dive into the formal syntax of the set.issubset() method.

set.issubset(set)
ArgumentData TypeExplanation
setA set or iterableThe set against which the elements of this set should be checked

Return Value of set.issubset()

The return value of set.issubset() is a Boolean whether the set is the subset of the set defined as an argument.

Advanced Examples set.issubset()

There are some subtleties you need to understand regarding the set subset method. Let’s dive into them by example!

We start with a simple and trivial example:

>>> {'Alice'}.issubset({'Alice', 'Bob'})
True

πŸ’‘ Can you also pass a list as an argument to the set.issubset() method? The answer is yes—the method takes any iterable.

>>> {'Alice'}.issubset(['Alice', 'Bob'])
True

A set is the subset of itself.

>>> s = {1, 2, 3}
>>> s.issubset(s)
True

This also means that two empty sets are the subsets of each other.

>>> set().issubset(set())
True

In fact, the empty set is a subset of every other set.

>>> set().issubset({1, 2, 3})
True

Can we pass multiple set arguments into the set.issubset() method? No! It only takes one argument.

>>> {1, 2, 3}.issubset({1, 2}, {3})
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    {1, 2, 3}.issubset({1, 2}, {3})
TypeError: issubset() takes exactly one argument (2 given)

To fix this TypeError, pass only one set argument into the set.issubset() method.

What is the Time Complexity of set.issubset() in Python?

The worst-case runtime complexity of the set.issubset() method for a set with n elements and a set argument with m elements is O(n) because you need to check for each set element whether it’s a member of the set argument.

You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:

Figure: The runtime complexity of set.issubset() increases linearly with increasing set sizes.

I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:

import matplotlib.pyplot as plt
import time

sizes = [i * 10**5 for i in range(50)]
runtimes = []

for size in sizes:
    t = set(range(size))
    s = set(range(0, size, 2))

    # Start track time ...
    t1 = time.time()
    s.issubset(t)
    t2 = time.time()
    # ... end track time
    
    runtimes.append(t2-t1)


plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')

plt.show()

Other Python Set Methods

All set methods are called on a given set. For example, if you created a set s = {1, 2, 3}, you’d call s.clear() to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.

add()Add an element to this set
clear()Remove all elements from this set
copy()Create and return a flat copy of this set
difference()Create and return a new set containing all elements of this set except the ones in the given set arguments. The resulting set has at most as many elements as any other.
difference_update()Remove all elements from this set that are members of any of the given set arguments.
discard()Remove an element from this set if it is a member, otherwise do nothing.
intersection()Create and return a new set that contains all elements that are members of all sets: this and the specified set(s).
intersection_update()Removes all elements from this set that are not members in all other specified sets.
isdisjoint()Return True if no element from this set is a member of any other specified set. Sets are disjoint if and only if their intersection is the empty set.
issubset()Return True if all elements of this set are members of the specified set argument.
issuperset()Return True if all elements of the specified set argument are members of this set.
pop()Remove and return a random element from this set. If the set is empty, it’ll raise a KeyError.
remove()Remove and return a specific element from this set as defined in the argument. If the set doesn’t contain the element, it’ll raise a KeyError.
symmetric_difference()Return a new set with elements in either this set or the specified set argument, but not elements that are members of both.
symmetric_difference_update()Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both.
union()Create and return a new set with all elements that are in this set, or in any of the specified set arguments.
update()Update this set with all elements that are in this set, or in any of the specified set arguments. The resulting set has at least as many elements as any other.