In this Python tutorial, we will learn the usage of Python set isdisjoint method properly. We’ll dive into a number of Python code examples so that we may grasp its role and proper usage in Python programs.
Introduction: Python Set Isdisjoint Method
This method returns a Boolean(True/False) value. It returns True if both the Python sets don’t have any item in common. If they do have one or more items in common then it returns False.
Let’s walk through detailed Python code examples to clarify working of this method. But for now, let’s have a detailed look at its syntax.
Syntax of isdisjoint() Method
setA.isdisjoint(setB)
- This method takes one argument. In this case, we’ve passed it a set. We can also pass iterable such as tuple, list, string or dictionary. If so, then this method will first convert the iterable to set and then checks whether they are disjoint or not.
- It returns True if both these sets have no item in common and returns False if they have one or more items in common.
Understanding Python Set Isdisjoint Method (Multiple Examples)
The following examples will demonstrate how to properly use isdisjoint() method.
Example 1: isdisjoint() Method Applied On Python Sets
sA = {2,3,5} sB = {6,8.9} print( sA.isdisjoint(sB) )
Output
True
- In this program, first we’ve created 2 Python sets.
- Then we’ve used the isdisjoint() method of first set and have passed second set as any argument to it.
- We also have used it inside print function to display its result. It shows True which means no common items were found between these two sets, i.e. they are disjoint.
Let’s now see another example in which this method will return False. See below:
sA = {1,3,5} sB = {5,8,1000} print( sA.isdisjoint(sB) )
Output
False
In this program, we have a common item (5) so the disjoint method returns False which can be seen in the above output block.
Example 2: isdisjoint() With No Argument
Let’s now try this method with no argument and see what happens. See below code:
a = {'aa','bb','cc'} print( a.isdisjoint() )
Output
TypeError: isdisjoint() takes exactly one argument (0 given)
It shows that one argument is required for this method. If not provided, then it’ll raise this type error exception just like it did in this program.
Example 3: Return Value Of Python Set Isdisjoint Method
Let’s now see what this method will return. See below:
a = {'aaa','bbb','ccc'} b = {'ddd','hhh','rrr'} rValue = a.isdisjoint(b) print( rValue )
Output
True
- For demonstration, we’ve created 2 Python sets.
- Then we’ve applied isdisjoint() method on them and store the return value of this method inside a variable.
- Now as a final action, we’ve printed the value of this variable and it shows True. It means that this method returns a Boolean value and True means that no common items were found between these 2 sets.
Example 4: Passing Iterable As An Argument To isdisjoint() Method
a = {1,2,3,4,5} print( a.isdisjoint([4,5,'a']) ) print( a.isdisjoint([9,7,8]) ) print( a.isdisjoint({'a':4,'b':5}) ) print( a.isdisjoint({'Python3'}) )
Output
False True True True
We’ve created a simple Python set. After that, we’ve used isdisjoint method and have passed Python list, tuple, dictionary and string as an argument to it. It’ll first convert them to set then it’ll see whether they are disjoint or not. The result can be seen in the above output section.
Example 5: Apply isdisjoint() Method on List of Sets Using For Loop
lsSetA=[{1,2,10},{'x','y','Zeeshan'},{223,4.56,21}] lsSetB=[{5,8,10},{'Zeeshan','w','x'},{212,4.58,22}] nLst=[] for val1,val2 in zip(lsSetA,lsSetB): nLst.append( {f'{val1},{val2}': val1.isdisjoint(val2)} ) for var in nLst: print(var)
Output
{'{1, 2, 10},{8, 10, 5}': False} {"{'x', 'y', 'Zeeshan'},{'x', 'w', 'Zeeshan'}": False} {'{4.56, 21, 223},{212, 4.58, 22}': True}
- In this code, we’ve created two Python list of sets. Also, we’ve created an empty set that’ll be used to store the resulting values.
- Then we’ve applied for loop which will take each item of these lists in every iteration. Click here to learn about the iteration over multiple Python lists simultaneously.
- Inside this Python for loop, we’ve applied isdisjoint method on the sets(items of lists). Also, we’ve created a logic that will store the sets as keys and the Boolean results as values inside Python dictionary. These dictionaries will be added to the new Python list using its append method.
- Finally, we’ve used another for loop just to print the list of dictionaries.
You can customize this program in your own way but don’t forget to share your experience with us in the comment section.
Hopefully, you have learned something new from this article. If so, then please share it with other Python developers.
Click here to learn more about Python set methods.
Feel free to ask us any further questions you may have concerning Python set isdisjoint method. We will be available to answer all your questions.
Conclusion
In conclusion, we’ve explored how Python set isdisjoint method works with sets or other iterables. Do implement it in your Python programs and don’t forget to share your worth reading feedback with us.
Detailed articles on Python programming are available on this site so feel free to check them out as well. Thank you for reading this article.