Python Tutorial

Python Set issubset() Method with Examples

TGTeam GyaniPanditNovember 16, 20263 min read

Hello friends! Remember subsets from your school mathematics? Python lets you check them in one line. The issubset() method checks whether every item of one set is also present in another set. If yes, the first set is a subset of the second, and the method returns True; otherwise it returns False. Let’s understand it with examples.

a.issubset(b)     # is every item of a also in b?
a <= b            # same check, operator form

Basic Example

a = {1, 2}
b = {1, 2, 3, 4}
print(a.issubset(b))
print(b.issubset(a))
print(a <= b)

Output:

True
False
True

1 and 2 are both in b, so a is a subset of b. But 3 and 4 are not in a, so b is not a subset of a.

Now here is a small detail that often confuses students.

Subset vs Proper Subset

A set is always a subset of itself. A proper subset is a subset that is smaller than the other set. Python uses < for proper subsets:

c = {1, 2}
print(a.issubset(c), a <= c, a < c)
print(a < b)

Output:

True True False
True

a and c are equal, so a is a subset of c but not a proper subset.

The Empty Set Is a Subset of Every Set

print(set().issubset({5}))

Output:

True

This matches the rule from school mathematics: the empty set φ is a subset of every set.

issubset() with Lists and Strings

The method accepts any iterable, but the <= operator needs sets on both sides:

print({1, 2}.issubset([1, 2, 3]))
try:
    print({1, 2} <= [1, 2, 3])
except TypeError as e:
    print("TypeError:", e)

print(set("cat").issubset(set("actions")))
print(set("dog").issubset("good"))

Output:

True
TypeError: '<=' not supported between instances of 'set' and 'list'
True
True

So far we asked “is A inside B?” But sometimes you want to ask the question the other way round.

issuperset(): The Reverse Check

If a is a subset of b, then b is a superset of a:

print(b.issuperset(a), b >= a)

Output:

True True

Read more: Python set issuperset()

Let’s now use this in a small real-life example.

Practical Example: Checking Required Subjects

required = {"maths", "science", "english"}
chosen = {"maths", "science", "english", "hindi"}
print(required.issubset(chosen))

Output:

True

The student has chosen all the required subjects (plus one extra), so the check passes. The same pattern works for checking that a user has all required permissions or that a form has all required fields.

Practice Question

What does print({1} <= {1}, {1} < {1}) print?

Answer: True False. A set is a subset of itself, but not a proper subset of itself.

So friends, to sum up: issubset() returns True if every item of the first set is in the second, otherwise False. The <= operator checks for a subset (equal sets count), while < checks for a proper subset, which must be strictly smaller. And just like in mathematics, the empty set is a subset of every set, so set().issubset(anything) is always True. Try the practice question once more on your own, and this topic will be crystal clear.

More set methods: Set methods in Python · isdisjoint() · union() · intersection()

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses