Open In App

Python Set isdisjoint() Method

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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

Example 2: Python Set isdisjoint() with Other Iterables as arguments

Python3




# Set
A = {2, 4, 5, 6}
 
# List
lis = [1, 2, 3, 4, 5]
 
# Dictionary dict, Set is formed on Keys
dict = {1: 'Apple', 2: 'Orange'}
 
# Dictionary dict2
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




# Python code to demonstrate
# isdisjoint method with blank
# sets
 
# defining empty set1
s1 = set()
 
# defining empty set2
s2 = set()
 
 
# using the isdisjoint method
# with empty sets
print(s1.isdisjoint(s2))


Output

True


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads