BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set isdisjoint() method with examples

Last Updated: April 1, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

Python Set isdisjoint() method checks whether the two sets are disjoint sets or not. If the sets are disjoint, this method returns true else it returns false. Two sets are said to be disjoint if they do not have any common elements. For example Set {1, 2, 3} and Set {4, 5, 6} are disjoint sets because they do not have any common elements.

Set isdisjoint() method Syntax

set.isdisjoint(iterable)

Parameter: It accepts any iterable such as Set, List, tuple, dictionary etc. as a parameter and converts it into a Set and then checks whether the Sets are disjoint or not.
Return Value: It returns a boolean value true or false. True if the sets are disjoint else it returns false.

Python Set isdisjoint() method Example

In the following example, we have three Sets X, Y and Z and we are checking whether set X & Y and X & Z are disjoint sets or not using isdisjoint() method. Since Set X and Y have common elements the isdisjoint() method returns false and because X and Z have no common elements, this method returns true for them.

# Set X
X = {4, 5}

# Set Y
Y = {4, 5, 6}

# Set Z
Z = {10, 20, 30}

print("X and Y are disjoint Sets?", X.isdisjoint(Y))
print("X and Z are disjoint Sets?", X.isdisjoint(Z))

Output:
Python isdisjoint() method example

Set isdisjoint() method with other iterable such as List, Tuple & Dictionary

When we pass the other iterable such as List, tuple and dictionary in isdisjoint() method, it converts them into a Set and then checks whether the sets are disjoint or not.

When converting a dictionary to a Set, the keys are considered as the elements of the converted set.

# Set A
A = {4, 5}

# List lis
lis = [10, 20, 30]

# Tuple t
t = (10, "hello")

# Dictionary dict, Set is formed on Keys
dict = {4 : 'Four', 5: 'Five'}

# Dictionary dict2
dict2 = {'Four': 4, 'Five': 5}

print("Set A and List lis disjoint?", A.isdisjoint(lis))
print("Set A and tuple t are disjoint?", A.isdisjoint(t))
print("Set A and dict are disjoint?", A.isdisjoint(dict))
print("Set A and dict2 are disjoint?", A.isdisjoint(dict2))

Output:
Python isdisjoint() method example with list, tuple and dictionary

Top Related Articles:

  1. Python Keywords and Identifiers with examples
  2. Python Variables with examples
  3. Python Set union() method with examples
  4. Python all() Function with examples
  5. Python abs() Function with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap