In this tutorial of Python, we’ll learn about Python set issubset method. Here, we’ll see what it is, its role in Python sets and how to properly use it. We’ll understand its usage using practical Python code examples for better understanding.
Introduction: Python Set IsSubset
This method returns a Boolean(True/False) value. It returns true if all the items of its calling set is present in the set passed to it as an argument. If not, then it returns false.
Let’s first make ourselves familiar with its syntax. After that, we’ll see its practical usage with the help of multiple code examples.
Syntax of issubset() Method
setA.issubset(setB)
- This method takes a single argument. It takes a set as an argument.
- This method will return true if all the items of set A are present in set B, if not then false will be returned.
- Return type of this method is of Boolean, i.e. either true or false.
Implementing Python Set issubset() Method (Multiple Examples)
Let’s now understand the practical implementation of this method.
Example 1: issubset() Method applied on Python Sets
setA = {1,2,3,4} setB = {2,3,4,5,1,10} print( setA.issubset(setB) )
Output
True
- We’ve specified 2 Python sets.
- Then we’ve called the issubset() method of set A and have passed set B as an argument to this method.
- It’ll check whether all the elements of set A are also present in set B or not. If present then it’ll return true, if not then it’ll return false.
- We can see that it returns true which means all the items of set A are also present in set B.
We can also switch the sets by calling the issubset method of set B and pass set A as an argument. See below code:
print(setB.issubset(setA))
Output
False
It returns false as it’ll check if all the items of set B are present in set A or not. If yes, then return true, else return false.
Example 2: No Argument Passed To issubset() Method
sA = {'a','b','d'} print( sA.issubset() )
Output
TypeError: issubset() takes exactly one argument (0 given)
In the above program, we haven’t passed any argument to issubset() method which results in a type error exception. It means that we must have to pass one argument to this method.
Example 3: Return Value Of Python Set IsSubset Method
stA = {'a','b','d'} stB = {'e','f','g','h'} retVal = stA.issubset(stB) print( retVal )
Output
False
- In this program, we’ve created two sets.
- After creating them, we’ve used issubset method of first set(stA in this case) and we’ve passed second set(stB in this case) as an argument to it.
- We’ve specified a variable which will store the return value of this method.
- Finally, we’ve printed value of the variable which shows a Boolean value. It means Python set issubset method returns a Boolean(True/False) value.
Example 4: issubset() Method Applied on List Of Python Sets Using For Loop
setLsA = [{1,2,3,4},{'a','b','c','d'},{2.3,(7,9),987}] setLsB = [{1,2,4,7,3,5},{'a','c','g','r'},{5.7,(8,10),658}] newList=[] for v1,v2 in zip(setLsA,setLsB): newList.append(f'{v1} is subset of {v2} : {v1.issubset(v2)}') for v in newList: print(v)
Output
{1, 2, 3, 4} is subset of {1, 2, 3, 4, 5, 7} : True {'d', 'c', 'a', 'b'} is subset of {'r', 'g', 'a', 'c'} : False {(7, 9), 2.3, 987} is subset of {658, (8, 10), 5.7} : False
- In this code example, we’ve first created 2 Python list of sets. We have also specified an empty list that will be used to store the resulting values.
- We’ve specified Python for loop which is used to take items from both lists. Click here to learn about the iteration over multiple Python lists simultaneously.
- This for loop will add the resulting values to new list using its append method after applying issubset method on them.
- We’ve used a second for loop just to show the items of new list. Result can be shown in the above output section.
We hope you now have learned the proper usage of Python set issubset method. If so, then do feel free to share it with other Python developers.
Click here to learn more about Python set methods.
Don’t hesitate to ask us any further questions you may have regarding Python set issubset method. We’ll be happy to answer all.
Conclusion
In conclusion, we’ve learned how to make use of Python set issubset method properly. Do implement it in other Python programs and don’t forget to share your experience with us.
Detailed articles on Python programming are available on this site so do feel free to visit them as well. Thank you for reading this article.