Set method

set.issubset()

Returns True when every element of this set is also in the other collection. Same as the <= operator for two sets.

IntermediateEvery example below runs in your browser. Change the code and press Run.

set.issubset(other)

a.issubset(b) asks: is everything in a also somewhere in b? If yes, a is a subset of b and the answer is True. Extra elements in b do not matter.

It is a one-line way to check permissions (does the user have all the roles this page needs?), required answers, or ingredients you have against a recipe.

A set is a subset of itself, and the empty set is a subset of everything. If you want "subset, but not equal", use the operator a < b.

Parameters

ParameterWhat it is
other
required
A set or other iterable to compare against.

Return value

True or False.

Examples

1Basic check

Pythoneditable
small = {1, 2}
big = {1, 2, 3, 4}
print(small.issubset(big))
print(big.issubset(small))
print(small <= big)
Output
True
False
True

2Can I cook this recipe?

Pythoneditable
recipe = {"flour", "eggs", "milk"}
pantry = {"flour", "eggs", "sugar", "butter"}
if recipe.issubset(pantry):
    print("You can cook it")
else:
    print("Missing:", sorted(recipe - pantry))
Output
Missing: ['milk']

3Does the user have every required role?

Pythoneditable
required = {"editor", "publisher"}
user_roles = ["viewer", "editor", "publisher"]
print(required.issubset(user_roles))
Output
True

The method accepts a list; the operator <= would need a set.

4Equal sets and the empty set

Pythoneditable
a = {1, 2}
print(a.issubset({1, 2}))
print(a < {1, 2})
print(set().issubset(a))
Output
True
False
True

< means "proper subset": a subset that is smaller than the other set.

Common mistakes

Mixing up the direction

a.issubset(b) checks that a is inside b. To check that a contains all of b, use set.issuperset() or b.issubset(a).

Using it on a string by accident

A string is a collection of characters, so {"a"}.issubset("cat") compares characters and gives True. Wrap whole words in a list or set.

Pythoneditable
print({"cat"}.issubset("cat"))
print({"cat"}.issubset(["cat", "dog"]))
Output
False
True

Questions people ask

How do I check if all items of one list are in another?

set(a).issubset(b), or all(x in b for x in a) if the items cannot go into a set.

What is the difference between <= and <?

a <= b allows the sets to be equal. a < b is only true when b has at least one extra element.

Spotted a mistake on this page? Tell us