{"id":25055,"date":"2021-11-10T09:37:22","date_gmt":"2021-11-10T04:07:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=25055"},"modified":"2021-11-10T09:37:22","modified_gmt":"2021-11-10T04:07:22","slug":"python-set-difference_update-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-difference_update-method-with-examples\/","title":{"rendered":"Python Set difference_update() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set difference_update() Method in Python:<\/strong><\/p>\n The difference_update() method removes items from both sets.<\/p>\n 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.<\/p>\n For Example<\/strong><\/p>\n 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.<\/p>\n The difference_update() replaces set P with the P-Q set difference.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is Required. The set used to look for differences.<\/p>\n Return Value:<\/strong><\/p>\n difference_update() produces a result None, indicating that the object (set) has been mutated.<\/p>\n Let P, Q be two sets<\/p>\n When you execute the code,<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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. …<\/p>\nset.difference_update(set)<\/pre>\n
\n
Given first set = {10, 11, 12, 13}\r\nGiven second set = {11, 12, 20, 14}<\/pre>\n
The given first set is : {10, 13}\r\nThe given second set is : {11, 12, 20, 14}\r\nThe result after applying difference_update method: None<\/pre>\n
Here it removes the common elements{11, 12} from the first set and\r\nupdates the given first set as {10, 13}.<\/pre>\n
Given first set = {20, 30, 40, 20, 30, 50, 60}\r\nGiven second set = {40, 50, 60, 100, 80}<\/pre>\n
The given first set is : {20, 30}\r\nThe given second set is : {100, 40, 80, 50, 60}\r\nThe result after applying difference_update method: None<\/pre>\n
Set difference_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, 11, 12, 13}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {11, 12, 20, 14}\r\n# Apply the difference_update() method to the given first and second sets.\r\n# Store it in another variable.\r\nrslt = fst_Set.difference_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 difference_update() method for the given sets.\r\nprint(\"The result after applying difference_update method: \", rslt)\r\n<\/pre>\n
The given first set is : {10, 13}\r\nThe given second set is : {11, 12, 20, 14}\r\nThe result after applying difference_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# Apply the difference_update() method to the given first and second sets.\r\n# Store it in another variable.\r\nrslt = fst_set.difference_update(scnd_set)\r\n# print the given first set.\r\nprint(\"The given first set is :\", fst_set)\r\n# print the second first set.\r\nprint(\"The given second set is :\", scnd_set)\r\n# Print the above result after applying difference_update method for the given sets.\r\nprint(\"The result after applying difference_update method: \", rslt)<\/pre>\n
Enter some random Set Elements separated by spaces = 10 20 15 16\r\nEnter some random Set Elements separated by spaces = 20 15 16 80\r\nThe given first set is : {10}\r\nThe given second set is : {16, 80, 20, 15}\r\nThe result after applying difference_update method: None<\/pre>\n","protected":false},"excerpt":{"rendered":"