Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
issubset() function in Python
In this article, we will learn the implementation and usage of issubset() function available in the Python Standard Library.
The issubset() method returns boolean True when all elements of a set are present in another set (passed as an argument) otherwise, it returns boolean False.
In the figure given below B is a subset of A. When A & B are identical sets, A is considered a proper subset of B, meaning both sets contain the same elements.
Syntax
<set1>.issubset(<set2>)
Return Value
boolean True/False
Example
Let's look at a basic example to understand the concept ?
A = {'t', 'u', 'o', 'r', 'i', 'a', 'l'}
B = {'t', 'u'}
C = {'p', 'o', 'i', 'n', 't'}
print("B.issubset(A):", B.issubset(A))
print("C.issubset(A):", C.issubset(A))
print("B.issubset(C):", B.issubset(C))
B.issubset(A): True C.issubset(A): False B.issubset(C): False
Explanation
Here, all elements of B ('t', 'u') are contained in A, so B.issubset(A) returns True. Set C contains elements like 'p' and 'n' that are not in A, so C.issubset(A) returns False.
Using Different Iterable Types
The issubset() method can accept other iterable types like lists, tuples, and strings as arguments ?
# Set to compare against
numbers = {1, 2, 3, 4, 5}
# Different iterable types
subset_list = [1, 2, 3]
subset_tuple = (1, 2)
subset_string = "12"
print("List subset:", numbers.issuperset(subset_list))
print("Tuple subset:", numbers.issuperset(subset_tuple))
# Using issubset with different types
small_set = {1, 2}
print("Set subset of list:", small_set.issubset([1, 2, 3, 4]))
print("Set subset of tuple:", small_set.issubset((1, 2, 3)))
List subset: True Tuple subset: True Set subset of list: True Set subset of tuple: True
Explanation
Python automatically converts the iterable arguments (lists, tuples) to sets for comparison. The calling object must always be a set, but the argument can be any iterable type.
Proper Subset vs Subset
Two identical sets are considered subsets of each other ?
set1 = {1, 2, 3}
set2 = {1, 2, 3}
set3 = {1, 2}
print("Identical sets - set1.issubset(set2):", set1.issubset(set2))
print("Identical sets - set2.issubset(set1):", set2.issubset(set1))
print("Proper subset - set3.issubset(set1):", set3.issubset(set1))
Identical sets - set1.issubset(set2): True Identical sets - set2.issubset(set1): True Proper subset - set3.issubset(set1): True
Comparison Table
| Method | Description | Example |
|---|---|---|
A.issubset(B) |
Check if A is subset of B |
{1,2}.issubset({1,2,3}) ? True |
A.issuperset(B) |
Check if A is superset of B |
{1,2,3}.issuperset({1,2}) ? True |
A <= B |
Subset operator (equivalent to issubset) |
{1,2} <= {1,2,3} ? True |
Conclusion
The issubset() method is useful for checking if all elements of one set exist in another set. It accepts various iterable types as arguments and returns True for identical sets, making it versatile for set comparison operations.
