Post

Python Set issuperset() Method

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

Python Set issuperset() Method

The Python set issuperset() method returns True if a set contains all elements of another set, and False otherwise. It can also be written using the >= operator.

The syntax of the issuperset() method is:

1
2
3
set.issuperset(set2)
# or
set1 >= set2

Python set issuperset() Parameters

The issuperset() method takes one parameter:

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

Here are examples demonstrating the issuperset() method:

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

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

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

The issuperset() method is useful for checking if one set contains another set entirely.

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