Skip to main content

Python Set isdisjoint() Function

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.

note

Sets are disjoint if and only if their intersection is the empty set!

Syntax

my_set.isdisjoint(set)

isdisjoint() Parameters

Python Set isdisjoint() function parameters:

ParameterConditionDescription
setRequiredA set to search for common items in
note

You can also pass iterables like list, tuple, dictionary or string. In that case, isdisjoint() first converts the iterables to sets and then checks if they are disjoint.

isdisjoint() Return Value

Python Set isdisjoint() function 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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a List B
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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a Tuple B
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.

note

The dictionary keys are used for isdisjoint(), not the values!

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create Dictionary B and C
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:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a String B
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