Python Set difference() Method with Examples

Prerequisite:

Python set() Function with Examples

Set difference() Method in Python:

The difference() method returns a set containing the difference of two sets.

The returned set contains items that only exist in the first set and not in both.

For Example:

If P and Q are two distinct sets. A set difference between P and Q is a set of elements that exist only in the set P but not in the set Q.

Let P={4,5,6,7}

Q={5,6,8,9}

P-Q={4,7}

Q-P ={8,9}

Syntax:

set.difference(set)

Parameters

set: This is Required. The set used to look for differences.

Return Value:

difference() computes the difference between two sets, each of which is a set. It makes no changes to the original sets.

Examples:

Example1:

Input:

Given first set = {10, 11, 12, 13}
Given second set = {11, 12, 20, 14}

Output:

firstset-secondset :  {10, 13}
secondset-firstset :  {20, 14}

Example2:

Input:

Given first set = {20, 30, 40, 20, 30, 50, 60}
Given second set = {40, 50, 60, 100, 80}

Output:

firstset-secondset :  {20, 30}
secondset-firstset :  {80, 100}

Set difference() Method with Examples in Python

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

1)Set Difference Using difference() method

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.
  • Get the difference between the( first set – second set ) using the difference() method.
  • Store it in another variable.
  • Get the difference between the (second set – first set ) using the difference() method.
  • Store it in another variable.
  • Print the result (first set – second set).
  • Print the result (second set – first set ).
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_Set = {10, 11, 12, 13}
# Give the second set as static input and store it in another variable.
scnd_set = {11, 12, 20, 14}
# Get the difference between the( first set - second set ) using the difference()
# method.
# Store it in another variable.
rslt1 = fst_Set.difference(scnd_set)
# Get the difference between the (second set - first set ) using the difference()
# method.
# Store it in another variable.
rslt2 = scnd_set.difference(fst_Set)
# Print the result (first set - second set).
print("firstset-secondset : ", rslt1)
# Print the result (second set - first set ).
print("secondset-firstset : ", rslt2)

Output:

firstset-secondset :  {10, 13}
secondset-firstset :  {20, 14}
2)Set Difference Using ‘-‘ Operator

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.
  • Get the difference between the( first set – second set ) using the ‘-‘ operator and print it.
  • Get the difference between the (second set – first set ) using the ‘-‘ operator 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 = {'u', 'v', 'w', 'x', 'y', 'u'}
# Give the second set as static input and store it in another variable.
scnd_set = {'w', 'z', 'x', 'a'}
# Get the difference between the( first set - second set ) using the '-' operator
# and print it
print("firstset-secondset : ", fst_set-scnd_set)
# Get the difference between the (second set - first set ) using the '-' operator
# print it.
print("secondset-firstset : ", scnd_set-fst_set)

Output:

firstset-secondset :  {'y', 'v', 'u'}
secondset-firstset :  {'z', 'a'}

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

1)Set Difference Using difference() method

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.
  • Get the difference between the( first set – second set ) using the difference() method.
  • Store it in another variable.
  • Get the difference between the (second set – first set ) using the difference() method.
  • Store it in another variable.
  • Print the result (first set – second set).
  • Print the result (second set – first set ).
  • 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()))
# Get the difference between the( first set - second set ) using the difference()
# method.
# Store it in another variable.
rslt1 = fst_set.difference(scnd_set)
# Get the difference between the (second set - first set ) using the difference()
# method.
# Store it in another variable.
rslt2 = scnd_set.difference(fst_set)
# Print the result (first set - second set).
print("firstset-secondset : ", rslt1)
# Print the result (second set - first set ).
print("secondset-firstset : ", rslt2)

Output:

Enter some random Set Elements separated by spaces = 20 30 40 10 10
Enter some random Set Elements separated by spaces = 60 50 20 30
firstset-secondset : {40, 10}
secondset-firstset : {50, 60}
2)Set Difference Using ‘-‘ Operator

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.
  • Get the difference between the( first set – second set ) using the ‘-‘ operator and print it.
  • Get the difference between the (second set – first set ) using the ‘-‘ operator 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()))
# Get the difference between the( first set - second set ) using the '-' operator
# and print it
print("firstset-secondset : ", fst_set-scnd_set)
# Get the difference between the (second set - first set ) using the '-' operator
# print it.
print("secondset-firstset : ", scnd_set-fst_set)

Output:

Enter some random Set Elements separated by spaces = 10 10 13 15
Enter some random Set Elements separated by spaces = 12 15
firstset-secondset : {10, 13}
secondset-firstset : {12}