Set issubset() Method in Python

Python Set issubset() Method

The issubset() method is used to check if a set is a subset of another set. A set is considered a subset of another set if all its elements are present in the other set.

Syntax of the issubset() method:

result = set1.issubset(set2)

Parameters:

  • set1: The set you want to check if it is a subset.
  • set2: The set you want to check against for being a superset.

Example:

# Creating two sets
set1 = {1, 2}
set2 = {1, 2, 3, 4, 5}

# Checking if set1 is a subset of set2
result = set1.issubset(set2)

print(result)  # Output: True (set1 is a subset of set2)

In this example, the issubset() method is applied to set1 and set2. Since all elements of set1 (1 and 2) are present in set2, the result is True, indicating that set1 is a subset of set2.

Let's see another example where set1 is not a subset of set2:

# Creating two sets
set1 = {1, 2, 6}
set2 = {1, 2, 3, 4, 5}

# Checking if set1 is a subset of set2
result = set1.issubset(set2)

print(result)  # Output: False (set1 is not a subset of set2)

In this case, set1 contains an element (6) that is not present in set2, so the issubset() method returns False, indicating that set1 is not a subset of set2.