The Wayback Machine - https://web.archive.org/web/20240623221628/https://www.geeksforgeeks.org/python-set-isdisjoint-method/
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


Previous Article
Next Article

Similar Reads

Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python Set clear() Method
Python Set clear() method removes all elements from the set. Python Set clear() Method Syntax: Syntax: set.clear() parameters: The clear() method doesn't take any parameters. Return: None Time complexity : The time complexity of set.clear() function on a set with n element is O(n) . Example 1: Python Set clear() Method Example C/C++ Code test_set =
2 min read
Python Set pop() Method
Python set pop() removes any random element from the set and returns the removed element. In this article, we will see about the Python set pop() method. Example Input: {9, 1, 0} Output: {9, 1} Explanation: By using set pop() method, a random element 0 is removed from the set and remaining set is returned.Python Set pop() SyntaxSyntax: set_obj.pop(
2 min read
Python Set - intersection_update() Method
Python intersection_update() method is used to update a set with common elements only of all the sets passed in parameter of intersection_update() method. Python set intersection_update() Method Syntax: Syntax: set.intersection_update(set1,set2,set3,...........,set n) Parameters: One or more sets Return: None Note: To perform this function, we have
2 min read
Python Set - remove() method
Python remove() Function is a built-in method to remove elements from the set. remove() method takes exactly one argument. Syntax set.remove(element) If the element passed to the remove() is present in the set then the element will be removed from the set. If the element passed to the remove() is not present in the set then KeyError Exception will
1 min read
Python Set issubset() Method
Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python. Python Set issubset() Method SyntaxSyntax: set_obj.issubset(other_set) Parameter: other_set: any other set to compare with.Return: bool Python set issubset() M
2 min read
Set add() Method in Python
The Python set add() method adds a given element to a set if the element is not present in the set in Python. Example: Add Element to an Empty setIt is used to add a new element to the empty set. C/C++ Code GEEK = set() GEEK.add('s') print("Letters are:", GEEK) # adding 'e' again GEEK.add('e') print("Letters are:", GEEK) # addin
3 min read
Class method vs Static method in Python
In this article, we will cover the basic difference between the class method vs Static method in Python and when to use the class method and static method in python. What is Class Method in Python? The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that
4 min read
Difference between Method Overloading and Method Overriding in Python
Method Overloading: Method Overloading is an example of Compile time polymorphism. In this, more than one method of the same class shares the same method name having different signatures. Method overloading is used to add more to the behavior of methods and there is no need of more than one class for method overloading.Note: Python does not support
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg