Python Disjoint Sets

Summary: in this tutorial, you’ll learn about disjoint sets and how to use the Python isdisjoint() method to check if two sets are disjoint.

Introduction to Python disjoint sets

Two sets are disjoint when they have no elements in common. In other words, two disjoint sets are sets whose intersection is an empty set.

For example, the {1,3,5} and {2,4,6} sets are disjoint because they have no common elements.

The following Venn diagram illustrates the disjoint sets:

In Python, you use the Set isdisjoint() method to check if two sets are disjoint or not:

set_a.isdisjoint(set_b)
Code language: Python (python)

The isdisjoint() method returns True if the set_a and set_b are disjoint. Otherwise, it returns False.

The isdisjoint() method also accepts any iterable, not just a set.

If you pass a list, a tuple, or a dictionary, the isdisjoint() method will convert it to a set before checking.

Python isdisjoint() method examples

The following example uses the isdisjoint() method to check if the set odd_numbers and set even_numbers are disjoint:

odd_numbers = {1, 3, 5} even_numbers = {2, 4, 6} result = odd_numbers.isdisjoint(even_numbers) print(result)
Code language: Python (python)

Output:

True
Code language: Python (python)

Since no elements in the odd_numbers are present in the set even_numbers, the isdisjoint() method returns True.

The following example uses the isdisjoint() method to check if the set letters and the set alphanumerics are disjoint:

letters = {'A', 'B', 'C'} alphanumerics = {'A', 1, 2} result = letters.isdisjoint(alphanumerics) print(result)
Code language: Python (python)

Output:

False
Code language: Python (python)

It returns False because the letter 'A' in the set alphanumerics is present in the set letters.

The following example passes a list to the isdisjoint() method instead of a set:

letters = {'A', 'B', 'C'} result = letters.isdisjoint([1, 2, 3]) print(result)
Code language: Python (python)

Output:

True
Code language: Python (python)

Summary

  • Two sets are disjoint if they have no element in common.
  • Use Python set isdisjoint() method to check if two sets are disjoint.
Did you find this tutorial helpful ?