Python Set issubset() Method

The issubset() method checks whether all elements of a set are present in another set.

Syntax

set1.issubset(set2)

Using The <= Operator

set1 <= set2

Parameters

set2 (required): The set to check against (can also be any iterable like list or tuple).

Return Value

Returns True if all elements of set1 are in set2, otherwise False.

Examples

Example 1: Basic Usage

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}

print(set1.issubset(set2)) # Output: True

Example 2: Not a subset

set1 = {1, 2, 3, 9}
set2 = {1, 2, 3, 4, 5}

print(set1.issubset(set2)) # Output: False

Example 3: Equal sets

A set is always considered a subset of itself.

set1 = {1, 2, 3, 4}
set2 = {1, 2, 3, 4}

print(set1.issubset(set2)) # Output: True

Example 4: With different iterable types

The issubset() method also works with iterables of different types, such as lists or tuples.

set1 = {1, 2, 3}
list1 = [1, 2, 3, 4, 5]

print(set1.issubset(list1)) # Output: True

Example 5: Empty set

The empty set is a subset of every set.

empty_set = set()
set2 = {1, 2, 3}

print(empty_set.issubset(set2)) # Output: True

Using The <= Operator

You can also use the <= operator to check for subsets.

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}

print(set1 <= set2) # Output: True

Proper Subset

A proper subset is a subset of another set, but the two sets are not equal. You can use the < operator to check whether a set is a proper subset.

For example:

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
set3 = {1, 2, 3}

print(set1 < set2) # Output: True
print(set1 < set3) # Output: False