Python Set isdisjoint() Method with Examples

Prerequisite:

Python set() Function with Examples

Set isdisjoint() Method in Python:

If none of the items are present in both sets, the isdisjoint() method returns true; otherwise, it returns False.

If two sets have no common elements, they are said to be disjoint.

For Example

p={1,2,3,4}

q={5,6,7}

Here p and q are said to be disjoint since they have no common elements.

Syntax:

set.isdisjoint(set)

Parameters

set: This is Required. The set to look for items that are similar.

Return Value:

This method returns

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

Examples:

Example1:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'p', 'q', 'r'}
Given third set = {'a', 'g'}

Output:

Check if first and second sets are disjoint:  True
Check if first and third sets are disjoint:  False
Check if second and third sets are disjoint:  True

Example2:

Input:

Given first set = {10, 20, 30, 40}
Given second set = {20, 70, 80}
Given third set = {100, 200, 300}

Output:

Check if first and second sets are disjoint:  False
Check if first and third sets are disjoint:  True
Check if second and third sets are disjoint:  True

Set isdisjoint() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Check if the first and second sets have any common elements using the isdisjoint() method and print it.
  • Check if the first and third sets have any common elements using the isdisjoint() method and print it.
  • Check if the second and third sets have any common elements using the isdisjoint() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {'a', 'b', 'c'}
# Give the second set as static input and store it in another variable.
scnd_set = {'p', 'q', 'r'}
# Give the third set as static input and store it in another variable.
thrd_set = {'a', 'g'}
# Check if the first and second sets have any common elements using the
# isdisjoint() method and print it.
print("Check if first and second sets are disjoint: ",
      fst_set.isdisjoint(scnd_set))
# Check if the first and third sets have any common elements using the
# isdisjoint() method and print it.
print("Check if first and third sets are disjoint: ",
      fst_set.isdisjoint(thrd_set))
# Check if the second and third sets have any common elements using the
# isdisjoint() method and print it.
print("Check if second and third sets are disjoint: ",
      scnd_set.isdisjoint(thrd_set))

Output:

Check if first and second sets are disjoint:  True
Check if first and third sets are disjoint:  False
Check if second and third sets are disjoint:  True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Check if the first and second sets have any common elements using the isdisjoint() method and print it.
  • Check if the first and third sets have any common elements using the isdisjoint() method and print it.
  • Check if the second and third sets have any common elements using the isdisjoint() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Check if the first and second sets has any common elements using the
# isdisjoint() method and print it.
print("Check if first and second sets are disjoint: ",
      fst_set.isdisjoint(scnd_set))
# Check if the first and third sets has any common elements using the
# isdisjoint() method and print it.
print("Check if first and third sets are disjoint: ",
      fst_set.isdisjoint(thrd_set))
# Check if the second and third sets has any common elements using the
# isdisjoint() method and print it.
print("Check if second and third sets are disjoint: ",
      scnd_set.isdisjoint(thrd_set))

Output:

Enter some random Set Elements separated by spaces = 10 20 30 40
Enter some random Set Elements separated by spaces = 20 70 80
Enter some random Set Elements separated by spaces = 100 200 300
Check if first and second sets are disjoint: False
Check if first and third sets are disjoint: True
Check if second and third sets are disjoint: True