Python Set isdisjoint() Method
Last Updated :
04 Apr, 2023
Python set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False. Two sets are said to be disjoint when their intersection is null.
Python set isdisjoint() Method Syntax:
Syntax: set1.isdisjoint(set2)
Parameters:
- another set to compare with
or
- an iterable (list, tuple, dictionary, and string)
Return: bool
Python Set isdisjoint() Method Example:
Python3
s1 = { 1 , 2 , 3 }
s2 = { 4 , 5 , 6 }
print (s1.isdisjoint(s2))
|
Output:
True
Example 1: Working with Set isdisjoint() Method
Python3
set1 = { 2 , 4 , 5 , 6 }
set2 = { 7 , 8 , 9 , 10 }
set3 = { 1 , 2 }
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
Example 2: Python Set isdisjoint() with Other Iterables as arguments
Python3
A = { 2 , 4 , 5 , 6 }
lis = [ 1 , 2 , 3 , 4 , 5 ]
dict = { 1 : 'Apple' , 2 : 'Orange' }
dict2 = { 'Apple' : 1 , 'Orange' : 2 }
print ( "Set A and List lis disjoint?" , A.isdisjoint(lis))
print ( "Set A and dict are disjoint?" , A.isdisjoint( dict ))
print ( "Set A and dict2 are disjoint?" , A.isdisjoint(dict2))
|
Output:
Set A and List lis disjoint? False
Set A and dict are disjoint? False
Set A and dict2 are disjoint? True
Example 3 : Both the sets are empty
Here we will see what will be the output if we use the isdisjoint() method with two sets both are empty.
Python3
s1 = set ()
s2 = set ()
print (s1.isdisjoint(s2))
|
Share your thoughts in the comments
Please Login to comment...