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:
Trueif the two sets are disjoint.Falseif 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: Trueoutput
TrueIf the two sets NOT disjoint, then isdisjoint() returns False:
A = {'Tom', 'Ryan', 'Anna'}
B = {'David', 'Anna', 'Paul'}
print(A.isdisjoint(B)) # Output: Falseoutput
FalseExample 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: Falseoutput
A and B are disjoint: FalseExample 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: Falseoutput
A and B are disjoint: FalseExample 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: Falseoutput
A and B are disjoint: True
A and C are disjoint: FalseExample 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: Falseoutput
A and B are disjoint: False