Post

Python Set issubset() Method

In this tutorial, we will understand about the python set issubset() method and its uses.

Python Set issubset() Method

The Python set issubset() method returns True if all elements of a set are present in another set, and False otherwise. It can also be written using the <= operator.

The syntax of the issubset() method is:

1
2
3
set.issubset(set2)
# or
set1 <= set2

Python set issubset() Parameters

The issubset() method takes one parameter:

  • set2: Another set or iterable to check if the original set is its subset.

Here are examples demonstrating the issubset() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Example 1: Basic subset check
set1 = {1, 2}
set2 = {1, 2, 3, 4, 5}
result = set1.issubset(set2)
print(result)  # Output: True

# Example 2: Using operator syntax
numbers1 = {1, 2, 3}
numbers2 = {1, 2}
result = numbers2 <= numbers1
print(result)  # Output: True

# Example 3: Equal sets are subsets
set1 = {1, 2, 3}
set2 = {1, 2, 3}
result = set1.issubset(set2)
print(result)  # Output: True

The issubset() method is useful for checking if one set is contained within another.

This post is licensed under CC BY 4.0 by the author.