Prerequisite:
Python set() Function with Examples
Set intersection_update() Method in Python:
The intersection_update() method removes items that do not appear in both sets (or in all sets if the comparison is done between more than two sets).
The intersection_update() method differs from the intersection() method in that the former returns a new set without the unwanted items, whereas the latter removes the unwanted items from the original set.
Syntax:
set.intersection_update(set1, set2,.....)
Parameters
set1: This is Required. The set to look for items that are similar in
set2: This is Optional. The other set to look for similar items in. You are free to compare as many sets as you want. Use a comma to separate the sets.
Return Value:
None is returned by this method (meaning it does not have a return value). It only calls the intersection_update() method to update the set.
Let P, Q, R be three sets
When you execute the code,
- the end result will be None
- P will be updated to the intersection of P, Q, and R.
- Q does not change.
- R remains constant.
Examples:
Example1:
Note: Here we are updating the third set.
Input:
Given first set = {10, 20, 30, 40, 50} Given second set = {100, 20, 80, 70, 30} Given third set = {200, 20, 80, 30}
Output:
The given first set is : {40, 10, 50, 20, 30} The given second set is : {100, 70, 80, 20, 30} The given third set is : {20, 30} The result after applying intersection_update() method: None
Explanation:
Here {20,30} are only the common elements in all the three sets. Hence we are updating the third set as {20, 30}
Example2:
Note: Here we are updating the first set.
Input:
Given first set = {'a', 'b', 'c'} Given second set = {'b', 'p', 'q'}
Output:
The given first set is : {'b'} The given second set is : {'p', 'q', 'b'} The result after applying intersection_update() method: None
Set intersection_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.
- Give the third set as static input and store it in another variable.
- Apply the intersection_update() method to the given first, second and third sets.
- Store it in another variable.
- Print the given first set.
- Print the given second set.
- Print the given third set.
- Print the above result after applying the intersection_update() method for the given three 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, 20, 30, 40, 50} # Give the second set as static input and store it in another variable. scnd_set = {100, 20, 80, 70, 30} # Give the third set as static input and store it in another variable. thrd_set = {200, 20, 80, 30} # Apply the intersection_update() method to the given first, second and third # sets. # Store it in another variable. rslt = thrd_set.intersection_update(fst_set, 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 given third set. print("The given third set is :", thrd_set) # Print the above result after applying intersection_update() method for the # given three sets. print("The result after applying intersection_update() method: ", rslt)
Output:
The given first set is : {40, 10, 50, 20, 30} The given second set is : {100, 70, 80, 20, 30} The given third set is : {20, 30} The result after applying intersection_update() method: None
Similarly, do the same for only two sets
# 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 = {'b', 'p', 'q'} # Apply the intersection_update() method to the given first and second sets. # Store it in another variable. rslt = fst_set.intersection_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 intersection_update() method for the # given two sets. print("The result after applying intersection_update() method: ", rslt)
Output:
The given first set is : {'b'} The given second set is : {'p', 'q', 'b'} The result after applying intersection_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.
- Give the third set as user input using set(),map(),input(),and split() functions.
- Store it in another variable.
- Apply the intersection_update() method to the given first, second and third sets.
- Store it in another variable.
- Print the given first set.
- Print the given second set.
- Print the given third set.
- Print the above result after applying the intersection_update() method for the given three 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())) # 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())) # Apply the intersection_update() method to the given first, second and third # sets. # Store it in another variable. rslt = thrd_set.intersection_update(fst_set, 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 given third set. print("The given third set is :", thrd_set) # Print the above result after applying intersection_update() method for the # given three sets. print("The result after applying intersection_update() method: ", rslt)
Output:
Enter some random Set Elements separated by spaces = 1 8 9 Enter some random Set Elements separated by spaces = 2 9 4 Enter some random Set Elements separated by spaces = 9 4 1 The given first set is : {8, 1, 9} The given second set is : {9, 2, 4} The given third set is : {9} The result after applying intersection_update() method: None