{"id":3389,"date":"2022-11-26T13:38:47","date_gmt":"2022-11-26T13:38:47","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3389"},"modified":"2022-11-26T13:38:48","modified_gmt":"2022-11-26T13:38:48","slug":"python-set-intersection","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/","title":{"rendered":"Python Set intersection() Method with many examples"},"content":{"rendered":"<p style=\"text-align: center;\">Python Set Intersection<\/p>\n<p>Now, we are going to learn about the intersection method. Basically, if you are familiar with the set intersection operation, then it would be very easy to get this method. But anyways, we would consider this method from the basics, so that you can follow it on the go.<\/p>\n<p>Let\u2019s say that we have set1 and set2. So, if we do set1.intersection(set2), then we get a new set, which contains the elements, which are in both, set1 and set2. Let\u2019s have a look at a simple example, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3390 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD.jpg\" alt=\"\" width=\"639\" height=\"97\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD.jpg 639w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD-300x46.jpg 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python Set intersection() Method<\/h2>\n<p>As you can see in the above program, we have two sets, set1, and set2, which have some elements. We are trying to do set1.intersection(set2). Doing this simply gets us a new set, which has the elements that are in both sets. Let\u2019s have a look at the output of the above program.<\/p>\n<p>{3, 5, 7, 11, 13}<\/p>\n<p>As you can see in the output, we got the set, which has the elements, which are there in both sets. So, at times, when we are required to perform the intersection operation on two or more sets(or there can be some other iterables as well, as arguments to the method), we can make use of the intersection method in our python program.<\/p>\n<p>Alternatively, we can also make use of the &amp; operator, to find the intersection of sets. Let\u2019s have a look at that as well.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3391 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD-1.jpg\" alt=\"\" width=\"646\" height=\"86\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD-1.jpg 646w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-intersection-METHOD-1-300x40.jpg 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>As you can see in the above program, we are trying to perform the intersection of two sets using the &amp; operator. If you try to execute the program and observe the output, you are going to get the same set as the previous output. You can have multiple sets here as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Set Intersection Now, we are going to learn about the intersection method. Basically, if you are familiar with the set intersection operation, then it would be very easy to get this method. But anyways, we would consider this method from the basics, so that you can follow it on the go. Let\u2019s say that [&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-3389","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\/3389","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=3389"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3389\/revisions"}],"predecessor-version":[{"id":4108,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3389\/revisions\/4108"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}