{"id":3422,"date":"2022-11-26T14:10:44","date_gmt":"2022-11-26T14:10:44","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3422"},"modified":"2022-11-26T14:10:45","modified_gmt":"2022-11-26T14:10:45","slug":"python-set-update","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-update\/","title":{"rendered":"Python Set update() Method with examples"},"content":{"rendered":"<p style=\"text-align: center;\">Python Set update<\/p>\n<p>Now, we are going to learn about the update method. Using the update method, we can update our current set, by adding the elements from another set, or some other iterable. For example, if we have set1 = {1, 2, 3}, and we have set2 = {3, 4, 5}, then doing set1.update(set2), will update the set1, by inserting the elements from the set2, and also if the element is already present there in the set, it is not added again. So, the set is updated with the appropriate values.<\/p>\n<p>Let\u2019s have a look at a simple program, which tries to demonstrate the same thing.<\/p>\n<h2 style=\"text-align: center;\">Python Set update() Method<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3423 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD.jpg\" alt=\"\" width=\"646\" height=\"94\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD.jpg 646w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD-300x44.jpg 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>As you can see in the above program, we have two sets, set1, and set2. Then we are trying to do set1.update(set2), so the set1 would be updated with the values from the set2. We can give some other iterable as well, as an argument. The thing is that set1 now contains the appropriate values.<\/p>\n<p>Let\u2019s have a look at the output in our case here &#8211;<\/p>\n<p>{1, 3.1, 4.1, 41, 10, 11, 45, 29}<\/p>\n<p>As you can see, set1 was updated with the items from set2. So, in this way, we can make use of the update method as and when required.\u00a0 Remember that with the update method, one set is updated with the values of some other set, or some other iterable.\u00a0 Let\u2019s try to have a look at another program, in which, we would pass some iterable to the update method. Let\u2019s have a look &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3424 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD-1.jpg\" alt=\"\" width=\"642\" height=\"117\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD-1.jpg 642w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-update-METHOD-1-300x55.jpg 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>As you can see in the above program, we have a set, and then we are trying to update the set, with the values from a list. So, we are passing a list as an argument to the update method. After that, we are printing the set. So, we can simply find that set1 was updated with the values from the list. Let\u2019s have a look at the output in our case.<\/p>\n<p>{1, 3.1, 10, 11, 12, 16, 29, 41, 45}<\/p>\n<p>As you can see, set1 is updated with the values from the list. So, as and when required, we can make use of the update method. Remember that with the update method, we can update a set with the values from some other set or iterable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Set update Now, we are going to learn about the update method. Using the update method, we can update our current set, by adding the elements from another set, or some other iterable. For example, if we have set1 = {1, 2, 3}, and we have set2 = {3, 4, 5}, then doing set1.update(set2), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3422","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3422"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3422\/revisions"}],"predecessor-version":[{"id":4117,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3422\/revisions\/4117"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}