.isdisjoint()

Published May 21, 2024
Contribute to Docs

In Python, the .isdisjoint() method checks whether two sets have any common elements. If no common elements exist, it returns True; otherwise, it returns False.

Syntax

set1.isdisjoint(set2)
  • set1: Represents the set to be compared.
  • set2: Represents the second set to be compared with set1 to check for common elements.

Example

The below example shows the usage of the .isdisjoint() method:

# Creating three sets
set1 = {1, 2, 3}
set2 = {4, 5, 6}
set3 = {3, 4, 5}
# Checking if 'set1' and 'set2' are disjoint
print(set1.isdisjoint(set2))
# Checking if 'set1' and 'set3' are disjoint
print(set1.isdisjoint(set3))

The above code produces the following output:

True
False

In the above example, the .isdisjoint() method returns True for set1 and set2 since they have no elements in common and False for set1 and set3 since they share the element 3.

Codebyte Example

Below is a code byte example demonstrating the use of the .isdisjoint() method:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy