Skip to content

Python Set isdisjoint() Method

How to use Python's isdisjoint() method to check if two sets have no common elements, including support for lists, tuples, dictionaries, and strings.

The Set isdisjoint() method returns True if two sets do not have any common items between them, i.e. they are disjoint. Otherwise, it returns False.

Syntax

my_set.isdisjoint(set)

isdisjoint() Parameters

Python Set isdisjoint() method parameters:

Parameter Condition Description
set Required A set to search for common items in

isdisjoint() Return Value

Python Set isdisjoint() method returns:

  • True if the two sets are disjoint.
  • False if the two sets are not disjoint.

Examples

Example 1: Check if Two Sets have No Items in Common

If the two sets are disjoint, then isdisjoint() returns True:

A = {'Tom', 'Ryan', 'Jack'}
B = {'David', 'Anna', 'Paul'}

print(A.isdisjoint(B)) # Output: True

output

True

If the two sets NOT disjoint, then isdisjoint() returns False:

A = {'Tom', 'Ryan', 'Anna'}
B = {'David', 'Anna', 'Paul'}

print(A.isdisjoint(B)) # Output: False

output

False

Example 2: Check if a Set is disjoint With a List

You can also pass iterables like list: isdisjoint() first converts the iterables to sets and then checks if they are disjoint.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = ['d', 'e', 'f']

print('A and B are disjoint:', A.isdisjoint(B)) # Output: A and B are disjoint: False

output

A and B are disjoint: False

Example 3: Check if a Set is disjoint With a Tuple

You can also pass iterables like tuple.: isdisjoint() first converts the iterables to sets and then checks if they are disjoint.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = ('d', 'e', 'f')

print('A and B are disjoint:', A.isdisjoint(B)) # Output: A and B are disjoint: False

output

A and B are disjoint: False

Example 4: Check if a Set is disjoint With a Dictionary

You can also pass iterables like dictionary: isdisjoint() first converts the iterables to sets and then checks if they are disjoint.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}

print('A and B are disjoint:', A.isdisjoint(B)) # Output: A and B are disjoint: True
print('A and C are disjoint:', A.isdisjoint(C)) # Output: A and C are disjoint: False

output

A and B are disjoint: True
A and C are disjoint: False

Example 5: Check if a Set is disjoint With a String

You can also pass iterables like string: isdisjoint() first converts the iterables to sets and then checks if they are disjoint.

For example:

A = {'a', 'e', 'i', 'o', 'u'}

B = 'def'

print('A and B are disjoint:', A.isdisjoint(B)) # Output: A and B are disjoint: False

output

A and B are disjoint: False
Navigation