{"id":25067,"date":"2021-11-10T09:37:41","date_gmt":"2021-11-10T04:07:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=25067"},"modified":"2021-11-10T09:37:41","modified_gmt":"2021-11-10T04:07:41","slug":"python-set-update-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-update-method-with-examples\/","title":{"rendered":"Python Set update() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set update() Method in Python:<\/strong><\/p>\n The update() method adds items from another set to the current set (or any other iterable).<\/p>\n If an item appears in both sets, only one appearance will appear in the updated set.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is Required. It is iterable to be inserted into the current set.<\/p>\n Return Value:<\/strong><\/p>\n None is returned by the set update() method (returns nothing).<\/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 <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisite: Python set() Function with Examples Set update() Method in Python: The update() method adds items from another set to the current set (or any other iterable). If an item appears in both sets, only one appearance will appear in the updated set. Syntax: set.update(set) Parameters set: This is Required. It is iterable to be …<\/p>\nset.update(set)<\/pre>\n
Given first set = {'a', 'b', 'c'}\r\nGiven second set = {'b', 'p', 'q'}<\/pre>\n
The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}\r\nThe given second set is : {'p', 'q', 'b'}\r\nThe result after applying the update() method: None<\/pre>\n
Here, add items of the second set to the first set and update it.\r\nBut 'b' appears in both sets, only one appearance of 'b' will appear in the \r\nupdated first set.<\/pre>\n
Given first set = {9, 11, 2, 6}\r\nGiven second set = {2, 1, 3, 9}<\/pre>\n
The given first set is : {9, 2, 11, 6}\r\nThe given second set after updating is : {1, 2, 3, 6, 9, 11}\r\nThe result after applying the update() method: None<\/pre>\n
Set 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 = {'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 update() method to the given first and second sets.\r\n# Store it in another variable.\r\nrslt = fst_set.update(scnd_set)\r\n# print the given first set.\r\nprint(\"The given first set after updating 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 the update() method for the\r\n# given two sets.\r\nprint(\"The result after applying the update() method: \", rslt)\r\n<\/pre>\n
The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}\r\nThe given second set is : {'p', 'q', 'b'}\r\nThe result after applying the 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\r\n# Apply the update() method to the given first and second sets.\r\n# Store it in another variable.\r\nrslt = scnd_set.update(fst_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 after updating is :\", scnd_set)\r\n# Print the above result after applying the update() method for the\r\n# given two sets.\r\nprint(\"The result after applying the update() method: \", rslt)\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 9 11 2 6\r\nEnter some random Set Elements separated by spaces = 2 1 3 9\r\nThe given first set is : {9, 2, 11, 6}\r\nThe given second set after updating is : {1, 2, 3, 6, 9, 11}\r\nThe result after applying the update() method: None<\/pre>\n