{"id":3386,"date":"2022-11-26T13:29:47","date_gmt":"2022-11-26T13:29:47","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3386"},"modified":"2022-11-26T13:29:49","modified_gmt":"2022-11-26T13:29:49","slug":"python-set-difference","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-difference\/","title":{"rendered":"Python Set difference() Method with many examples"},"content":{"rendered":"<p style=\"text-align: center;\">Python Set difference<\/p>\n<p>Now, we are going to have a look at the difference method. With the difference method, we get the difference of the two or more sets (we can also provide some other iterable as an argument to the method). Basically, Let\u2019s understand what is difference first. Let\u2019s consider that we have set1 and set2. Now, if we do set1.difference(set2), then we are going to get a new set, in which, we would have the elements that are there in set1, but not in set2. In other words, we get the elements that are unique to set1.<\/p>\n<p>Let\u2019s have a look at a simple program, which demonstrates using difference methods.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3387 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference-METHOD.jpg\" alt=\"\" width=\"638\" height=\"115\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference-METHOD.jpg 638w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-difference-METHOD-300x54.jpg 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python Set difference method<\/h2>\n<p>As you can see in the above program, we have two sets, set1, and set2. The set1 has some odd numbers. On the other hand, set2 has some prime numbers. Then, we are doing set1.difference(set2). Basically, with this, we get a new set, which contains the elements which are in set1, but not in set2. In short, we get the elements in the new set, which are unique to the set1.<\/p>\n<p>If you try to execute the above program, the output of the above program comes out to be something like this &#8211;<\/p>\n<p>{1, 9}<\/p>\n<p>As you can see, we get a set, which contains the elements, which are unique to set1. In this way, we can make use of the difference method in our python programs, as and when required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Set difference Now, we are going to have a look at the difference method. With the difference method, we get the difference of the two or more sets (we can also provide some other iterable as an argument to the method). Basically, Let\u2019s understand what is difference first. Let\u2019s consider that we have set1 [&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-3386","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\/3386","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=3386"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3386\/revisions"}],"predecessor-version":[{"id":4107,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3386\/revisions\/4107"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}