{"id":3413,"date":"2022-11-26T13:59:06","date_gmt":"2022-11-26T13:59:06","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3413"},"modified":"2022-11-26T13:59:07","modified_gmt":"2022-11-26T13:59:07","slug":"python-set-difference-update","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-difference-update\/","title":{"rendered":"Python Set difference_update() Method with examples"},"content":{"rendered":"<p style=\"text-align: center;\">Python Set difference_update<\/p>\n<p>Now, we are going to learn about the difference_update method. Well, we are quite familiar with the concept of the difference of the sets, and the difference method. In the difference method, we simply calculate the difference of the set.<\/p>\n<p>Let\u2019s quickly understand what we mean by the difference in the set. Let\u2019s say that we have two sets set1 and set2, so the set1.difference(set2) is going to return a new set, which contains the elements, which are there in the set1, but not in the set2, which simply means that the new set contains elements which are unique to set1.<\/p>\n<h2 style=\"text-align: center;\">Python Set difference_update() Method<\/h2>\n<p>So, the difference_update method does the same thing. But if we are doing set1.difference_update(set2), then we are not going to get a new set, instead, the updates will be made to set1 itself. So, this method is not going to return a new set, but the changes will be made on set1 in the above case. We can also say that doing a set1.difference_update(set2), will simply remove all the elements from set1, which are also there in set2.<\/p>\n<p>Let\u2019s have a look at the below program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3414 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference_update-METHOD.jpg\" alt=\"\" width=\"652\" height=\"95\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference_update-METHOD.jpg 652w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference_update-METHOD-300x44.jpg 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>As you can see in the above program, we have two sets, set1, and set2. Then we are trying to use the method difference_update. Since we are doing set1.difference_update(set2), the changes would be made to set1, and we can say that all the elements in set2, will be removed from set1. After that, we are printing set1, so let\u2019s have a look at the output as well.<\/p>\n<p>{29, 87, 31}<\/p>\n<p>As you can see, we have set1 as output, and all the elements that are there in set2 were removed from set1. In this way, you can make use of the difference_update method as and when required in the program. Remember that here, we are not going to get a new set, but the changes will be made to set1 if we are doing set1.difference_update(set2).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Set difference_update Now, we are going to learn about the difference_update method. Well, we are quite familiar with the concept of the difference of the sets, and the difference method. In the difference method, we simply calculate the difference of the set. Let\u2019s quickly understand what we mean by the difference in the set. [&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-3413","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\/3413","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=3413"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3413\/revisions"}],"predecessor-version":[{"id":4114,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3413\/revisions\/4114"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}