Python Set isdisjoint() method
Python Set isdisjoint() method
The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns False.
Syntax
set.isdisjoint(set1)
Parameter
set- This parameter represents the main set.
set1- This argument signifies the other set to search for equal items.
Return
This method returns a boolean value True if two sets are disjoint sets, else it returns False.
Example 1
# Python program explaining # the set.isdisjoint() method #initializing set1 set1 = {"mango", "banana", "apple","tomato"} print("Set 1:",set1) # initializing set2 set2 = {"potato", "spinach","Carrot", "apple"} print("Set 2:",set2) # will search for common items in both the sets, value= set1.isdisjoint(set2) # will return false as the element "apple" is common in both sets print("The isdisjoint() method returns:",value)
Output
Set 1: {'banana', 'tomato', 'apple', 'mango'} Set 2: {'potato', 'apple', 'Carrot', 'spinach'} The isdisjoint() method returns: False
Example 2
# Python program explaining # the set.isdisjoint() method # initializing set 1 set1 = {1,2,3, 4, 5, 9,10} # initializing set 2 set2 = {2,3,4,7, 8, 10} # initializing set 3 set3 = {1,5,54} #checking of disjoint for set1 and set2 # will return false, as there are common elements between set1 and set2 print("set1 and set2 are disjoint: ", set1.isdisjoint(set2)) #checking of disjoint for set2 and set3 # will return true as no common element between set2 and set3 print("set2 and set3 are disjoint: ", set2.isdisjoint(set3))
Output
set1 and set2 are disjoint: False set2 and set3 are disjoint: True