issubset Python determines whether one set is a subset of another. It returns True if the supplied set exists in the other set (passed in the argument); otherwise, it returns False. It has no bearing on the provided set. Consider the following two sets of numbers: A: 1, 2, 3 and B: 1, 2, 3, 4. We can use the following method to determine if A is a subset of B: Because all of the items in set A are present in set B, this method returns true, indicating that A is a subset of B.
Return Value from issubset()
The value of a issubset Python is that of a Boolean expression: this method returns true or false depending on whether the calling set is a subset of the set supplied as a parameter. If the calling set is a subset of the set passed as a parameter, this method returns true; otherwise, it returns false.
Example: How issubset() works?
We have three set numbers in the following example, and we’re using the issubset() method to see if they’re subsets of each other. X is a subset of Y because all of Set X’s elements are present in Set Y, and Z is a subset of Y because all of Set Z’s elements are present in Set Y.
# Set X
X = {1, 2, 3}
# Set Y
Y = {1, 2, 3, 4}
# Set Z
Z = {3, 4}
print("X is a subset of Y?", X.issubset(Y))
print("X is a subset of Z?", X.issubset(Z))
print("Y is a subset of X?", Y.issubset(X))
print("Z is a subset of Y?", Z.issubset(Y))
Output:
X is a subset of Y? True
True X is a subset of Z? False
False Y is a subset of X? False
False Z is a subset of Y? True
Process finished with exit code 0
Questions
What is issubset in Python?
Python Set issubset() is a Python method that determines whether a given set is a subset of another. If the supplied set is present in the other set (passed in the argument), it returns True; otherwise, it returns False. It has no effect on the supplied set.Â
How do I use Issubset in Python?
This method takes an iterable (list, tuple, dictionary, set, and string) as an argument, and if iterable other than set is supplied, it converts the iterable object to a set object before checking whether all items of a set are also present in a set ( passed as an argument ). If the answer is yes, it will return True; if the answer is no, it will return False.
Syntax : < Set Object >.issubset( <iterable object> )Â
What is a superset in Python?
If a set contains all of the items of another set, the issuperset() method returns True (passed as an argument). If this is not the case, False is returned. If all elements of Y exist in X, it is said that X is the superset of Y.
How do you intersect in Python?
The intersection() method returns a set containing the similarities of two or more sets.
Syntax : set.intersection(set1, set2 … etc)
Leave a Reply