Prerequisite:
Python set() Function with Examples
Set difference_update() Method in Python:
The difference_update() method removes items from both sets.
The difference_update() method differs from the difference() method in that the difference() method returns a new set that does not include the unwanted items, whereas the difference_update() method removes the unwanted items from the original set.
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 set P but not in set Q.
The difference_update() replaces set P with the P-Q set difference.
Syntax:
set.difference_update(set)
Parameters
set: This is Required. The set used to look for differences.
Return Value:
difference_update() produces a result None, indicating that the object (set) has been mutated.
Let P, Q be two sets
When you execute the code,
- The result is None.
- P will be updated to P-Q.
- Q will remain unchanged.
Examples:
Example1:
Input:
Given first set = {10, 11, 12, 13} Given second set = {11, 12, 20, 14}
Output:
The given first set is : {10, 13} The given second set is : {11, 12, 20, 14} The result after applying difference_update method: None
Explanation:
Here it removes the common elements{11, 12} from the first set and updates the given first set as {10, 13}.
Example2:
Input:
Given first set = {20, 30, 40, 20, 30, 50, 60} Given second set = {40, 50, 60, 100, 80}
Output:
The given first set is : {20, 30} The given second set is : {100, 40, 80, 50, 60} The result after applying difference_update method: None
Set difference_update() 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.
- Apply the difference_update() method to the given first and second sets.
- Store it in another variable.
- Print the given first set.
- Print the second set.
- Print the above result after applying the difference_update() method for the given sets.
- 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} # Apply the difference_update() method to the given first and second sets. # Store it in another variable. rslt = fst_Set.difference_update(scnd_set) # print the given first set. print("The given first set is :", fst_Set) # print the given second set. print("The given second set is :", scnd_set) # Print the above result after applying difference_update() method for the given sets. print("The result after applying difference_update method: ", rslt)
Output:
The given first set is : {10, 13} The given second set is : {11, 12, 20, 14} The result after applying difference_update method: None
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.
- Apply the difference_update() method to the given first and second sets.
- Store it in another variable.
- Print the given first set.
- Print the second set.
- Print the above result after applying the difference_update() method for the given sets.
- 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())) # Apply the difference_update() method to the given first and second sets. # Store it in another variable. rslt = fst_set.difference_update(scnd_set) # print the given first set. print("The given first set is :", fst_set) # print the second first set. print("The given second set is :", scnd_set) # Print the above result after applying difference_update method for the given sets. print("The result after applying difference_update method: ", rslt)
Output:
Enter some random Set Elements separated by spaces = 10 20 15 16 Enter some random Set Elements separated by spaces = 20 15 16 80 The given first set is : {10} The given second set is : {16, 80, 20, 15} The result after applying difference_update method: None