The issubset() function in python helps to check whether the set is a subset or not. The set is said to be a subset, all elements of this set are present in another set. This method returns true if the set is a subset otherwise it returns false.
A.issubset(B) #where A and B are sets
The issubset() function takes a set as its parameter. Set A is said to be a subset of B only if all the elements of A must be in B.
Parameter | Description | Required / Optional |
---|---|---|
A & B | The set of items to search for | Required |
The return value of this method must be a boolean. Another way of checking subset is by using subset operator (<=) like set_a <= set_b. Checking 'set_a' is a subset of ' set_b'.
Input | Return Value |
---|---|
A is a subset of B | True |
A is not a subset of B | False |
A = {'a', 'b', 'c', 'd', 'e'}
B = {'a', 'b', 'c'}
C = {'b', 'a', 'e','d'}
# Returns False
print(A.issubset(B))
# Returns True
# B is a subset of A
print(B.issubset(A))
# Returns False
print(A.issubset(C))
# Returns True
print(C.issubset(A))
Output:
False True False True
# Set X
X = {1, 2, 3}
# Set Y
Y = {1, 2, 3, 4}
print("Y is a subset of X?", Y.issubset(X))
print("X is a subset of Y?", X.issubset(Y))
Output:
Y is a subset of X? False X is a subset of Y? True