Skip to content
Related Articles

Related Articles

Improve Article
isdisjoint() function in Python
  • Last Updated : 06 Feb, 2018

Two sets are said to be disjoint when their intersection is null. In simple words they do not have any common element in between them.

Examples:

Let set A = {2, 4, 5, 6}
and set B = {7, 8, 9, 10} 

set A and set B are said to be be disjoint sets as their
intersection is null. They do-not have any common elements in between them. 

Syntax:

set1.isdisjoint(set2)

Parameters:



The isdisjoint() method takes only a single argument.
It can also take an iterable (list, tuple, dictionary and string) to disjoint(). The isdisjoint() method will automatically convert iterables to set and checks whether the sets are disjoint or not.

Return Value:

returns Trueif the two sets are disjoint.
returns Falseif the twos sets are not disjoint.

Below is the Python3 implementation of the above approach:




# Python3 program for isdisjoint() function
  
set1 = {2, 4, 5, 6
set2 = {7, 8, 9, 10}
set3 = {1, 2}
  
  
#checking of disjoint of two sets
print("set1 and set2 are disjoint?", set1.isdisjoint(set2))
  
print("set1 and set3 are disjoint?", set1.isdisjoint(set3))

Output:

set1 and set2 are disjoint? True
set1 and set3 are disjoint? False

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :