disjoint() method of set in Python

The set class offers a disjoint() method that determines whether the intersection between two sets is a null set. The Python example checks whether the set of woodwinds and the set of guitars are disjoint to each other.

Set Updated 22 Jul 2021 1 min read

Overview

  • The method disjoint() determines whether two sets does not have any element in common.
  • The disjoint() method checks the disjoint property between two sets.
  • In set theory, two sets are said to be disjoint if they do not have any element in common. When the intersection between two sets is an empty set, they are disjoint to each other. If A ∩ B = Ø then the sets A and B are called disjoint.

Example

python
# Example Python program that creates two sets

            # and finds whether they are disjoint

            woodwind = {"Piccolo", "Flute", "Oboe", "Clarinet", "Cor Anglais",

                        "Bassoon", "Contra Bassoon"}

            guitars = {"Balalaika", "Mexican Mariachi Guitar",

                       "Mandolin", "Ukulele", "Calssical guitar", "Electric Bass Guitar",

                       "Electric Guitar"}

hasCommon = woodwind.isdisjoint(guitars)

            print("Woodwinds and guitars have no instruments common to both:%s"%hasCommon)

Output

Output
Woodwinds and guitars have no instruments common to both:True