Python Set issubset() method
Python Set issubset() method
The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False.
Syntax
set.issubset(set1)
Parameter
set- This parameter represents the set to check whether it is a subset of set1 or not.
set1- This argument signifies the other set.
Return
This method returns a Boolean value True if all items in the set exists in the specified set, otherwise it returns False.
Example 1
# Python program explaining
# the set.issubset() method
#initializing set1
set1 = {"mango", "banana", "apple", "tomato"}
print("Set 1:",set1)
# initializing set2
set2 = {"mango", "banana"} # subset of set1
print("Set 2:",set2)
# will search whether all the elements of set2 are present in set1 or not
value= set2.issubset(set1)
# will return True
print("Set 2 is a subset of Set 1:",value)
Output
Set 1: {'mango', 'tomato', 'apple', 'banana'}
Set 2: {'mango', 'banana'}
Set 2 is a subset of Set 1: True
Example 2
# Python program explaining
# the set.issubset() method
# Initializing set A
A = {"a", "e", "c"}
# initializing set B
B = {"a", "b", "c","d", "e"}
# initializing set C
C = {"a", "b", "d", "e"}
# Returns True
print("A is a subset of B: ",A.issubset(B))
# Returns False
# As Set B is not subset of Set A
print("B is a subset of A: ",B.issubset(A))
# Returns False
print("A is a subset of C: ",A.issubset(C))
# Returns True
print("C is a subset of B: ",C.issubset(B))
Output
A is a subset of B: True B is a subset of A: False A is a subset of C: False C is a subset of B: True
