ReferenceSet methods

Python Set issubset() Method

Short answer
a.issubset(b) returns True if every element of a is also in b, otherwise False. It is the same test as a <= b.

Subset checks tell you whether one group is fully covered by another: are all required skills among a candidate's skills, or are all items in the cart in stock? It is a yes/no question, so the answer is a boolean.

An empty set is a subset of every set, and every set is a subset of itself. To test the opposite direction use issuperset(); to test that two sets share nothing use isdisjoint(). See also the related set operations such as intersection.

Quick facts
Syntaxa.issubset(other) or a <= b
Parametersother: an iterable (the method converts it to a set)
ReturnsTrue or False
Changes the original?No
Time complexityO(len(a)) after other is a set

How set.issubset() works

Python checks each element of a for membership in other and stops at the first miss.

Python
required = {"python", "sql"}
skills = {"python", "sql", "git"}
print(required.issubset(skills))
print(skills.issubset(required))

Subset, proper subset, superset and disjoint

A proper subset (<) must be strictly smaller, so equal sets are subsets but not proper subsets. The method accepts any iterable, while the operators need sets.

QuestionMethodOperator{1, 2} and {1, 2, 3}
Is a inside b?a.issubset(b)a <= bTrue
Is a strictly inside b?nonea < bTrue
Does a contain all of b?a.issuperset(b)a >= bFalse
Do they share nothing?a.isdisjoint(b)noneFalse
Are they equal?nonea == bFalse

Examples you can run

Press Run, then edit the code and run it again.

Does the candidate have all required skills

The check passes only when no required skill is missing.

Does the candidate have all required skills
required = {"python", "sql"}
skills = {"python", "sql", "git"}
print(required.issubset(skills))
print({"python", "java"}.issubset(skills))
Expected outputTrue
False

Edge cases: empty set, equal sets and lists

The empty set is a subset of everything, and a list argument is accepted by the method.

Edge cases: empty set, equal sets and lists
a = {1, 2}
print(set().issubset(a))
print(a.issubset({1, 2}))
print(a < {1, 2})
print(a.issubset([1, 2, 3]))
Expected outputTrue
True
False
True

Common mistakes

Mixing up the direction

✗ Wrong

cart = {"pen", "book"}
stock = {"pen", "book", "bag"}
print(stock.issubset(cart))
False (but you wanted to know whether the cart fits in stock)

✓ Right

cart = {"pen", "book"}
stock = {"pen", "book", "bag"}
print(cart.issubset(stock))

The set you call the method on must be the smaller one: cart.issubset(stock).

Using <= with a list

✗ Wrong

print({1, 2} <= [1, 2, 3])
TypeError: '<=' not supported between instances of 'set' and 'list'

✓ Right

print({1, 2}.issubset([1, 2, 3]))

Use issubset() or convert the list with set().

Python Set issubset() Method: questions people ask

Is a set a subset of itself in Python?

Yes. s.issubset(s) is True, but s < s is False because a proper subset must be strictly smaller.

Is the empty set a subset of every set?

Yes. set().issubset(anything) is always True, because the empty set has no elements that could be missing.

How do I check if a list contains all items of another list?

Convert the needed items to a set and call set(needed).issubset(available).

Keep learning

Step-by-step lessons:
Python Set OperationsPython Sets

Related:
Python Set intersection() MethodPython Set difference() MethodPython Set union() Method

More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set intersection() MethodPython Set difference() MethodPython Set symmetric_difference() Method See all →