{"id":25058,"date":"2021-11-10T09:37:32","date_gmt":"2021-11-10T04:07:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=25058"},"modified":"2021-11-10T09:37:32","modified_gmt":"2021-11-10T04:07:32","slug":"python-set-intersection_update-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-intersection_update-method-with-examples\/","title":{"rendered":"Python Set intersection_update() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set intersection_update() Method in Python:<\/strong><\/p>\n 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).<\/p>\n 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.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set1:<\/strong> This is Required. The set to look for items that are similar in<\/p>\n set2:<\/strong> 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.<\/p>\n Return Value:<\/strong><\/p>\n 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.<\/p>\n Let P, Q, R be three sets<\/p>\n When you execute the code,<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Note:<\/strong> Here we are updating the third set.<\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Note:<\/strong> Here we are updating the first set.<\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\nset.intersection_update(set1, set2,.....)<\/pre>\n
\n
Given first set = {10, 20, 30, 40, 50}\r\nGiven second set = {100, 20, 80, 70, 30}\r\nGiven third set = {200, 20, 80, 30}<\/pre>\n
The given first set is : {40, 10, 50, 20, 30}\r\nThe given second set is : {100, 70, 80, 20, 30}\r\nThe given third set is : {20, 30}\r\nThe result after applying intersection_update() method: None<\/pre>\n
Here {20,30} are only the common elements in all the three sets.\r\nHence we are updating the third set as {20, 30}<\/pre>\n
Given first set = {'a', 'b', 'c'}\r\nGiven second set = {'b', 'p', 'q'}<\/pre>\n
The given first set is : {'b'}\r\nThe given second set is : {'p', 'q', 'b'}\r\nThe result after applying intersection_update() method: None<\/pre>\n
Set intersection_update() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {10, 20, 30, 40, 50}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {100, 20, 80, 70, 30}\r\n# Give the third set as static input and store it in another variable.\r\nthrd_set = {200, 20, 80, 30}\r\n# Apply the intersection_update() method to the given first, second and third\r\n# sets.\r\n# Store it in another variable.\r\nrslt = thrd_set.intersection_update(fst_set, scnd_set)\r\n# print the given first set.\r\nprint(\"The given first set is :\", fst_set)\r\n# print the given second set.\r\nprint(\"The given second set is :\", scnd_set)\r\n# print the given third set.\r\nprint(\"The given third set is :\", thrd_set)\r\n# Print the above result after applying intersection_update() method for the\r\n# given three sets.\r\nprint(\"The result after applying intersection_update() method: \", rslt)\r\n<\/pre>\n
The given first set is : {40, 10, 50, 20, 30}\r\nThe given second set is : {100, 70, 80, 20, 30}\r\nThe given third set is : {20, 30}\r\nThe result after applying intersection_update() method: None<\/pre>\n
Similarly, do the same for only two sets<\/h5>\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {'a', 'b', 'c'}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {'b', 'p', 'q'}\r\n# Apply the intersection_update() method to the given first and second sets.\r\n# Store it in another variable.\r\nrslt = fst_set.intersection_update(scnd_set)\r\n# print the given first set.\r\nprint(\"The given first set is :\", fst_set)\r\n# print the given second set.\r\nprint(\"The given second set is :\", scnd_set)\r\n# Print the above result after applying intersection_update() method for the\r\n# given two sets.\r\nprint(\"The result after applying intersection_update() method: \", rslt)\r\n<\/pre>\n
The given first set is : {'b'}\r\nThe given second set is : {'p', 'q', 'b'}\r\nThe result after applying intersection_update() method: None<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first set as user input using set(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nfst_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the second set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nscnd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the third set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nthrd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n \r\n# Apply the intersection_update() method to the given first, second and third\r\n# sets.\r\n# Store it in another variable.\r\nrslt = thrd_set.intersection_update(fst_set, scnd_set)\r\n# print the given first set.\r\nprint(\"The given first set is :\", fst_set)\r\n# print the given second set.\r\nprint(\"The given second set is :\", scnd_set)\r\n# print the given third set.\r\nprint(\"The given third set is :\", thrd_set)\r\n# Print the above result after applying intersection_update() method for the\r\n# given three sets.\r\nprint(\"The result after applying intersection_update() method: \", rslt)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 1 8 9\r\nEnter some random Set Elements separated by spaces = 2 9 4\r\nEnter some random Set Elements separated by spaces = 9 4 1\r\nThe given first set is : {8, 1, 9}\r\nThe given second set is : {9, 2, 4}\r\nThe given third set is : {9}\r\nThe result after applying intersection_update() method: None<\/pre>\n","protected":false},"excerpt":{"rendered":"