{"id":113808,"date":"2020-06-05T17:20:32","date_gmt":"2020-06-05T17:20:32","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9263"},"modified":"2020-06-05T17:20:32","modified_gmt":"2020-06-05T17:20:32","slug":"how-to-intersect-multiple-sets-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/05\/how-to-intersect-multiple-sets-in-python\/","title":{"rendered":"How to Intersect Multiple Sets in Python?"},"content":{"rendered":"<p class=\"has-background has-luminous-vivid-amber-background-color\"><strong>To intersect multiple sets, stored in a list <code>l<\/code>, use the Python one-liner <code>l.pop().intersection(*l)<\/code> that takes the first set from the list, calls the <code>intersection()<\/code> method on it, and passes the remaining sets as arguments by unpacking them from the list.<\/strong><\/p>\n<p>A <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\">set is a unique collection of unordered elements<\/a>. The intersection operation creates a new set that consists of the elements that exist in all sets.<\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/intersect-1024x576.jpg\" alt=\"\" class=\"wp-image-9277\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/intersect-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/intersect-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/intersect-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p>So, let&#8217;s dive into the formal problem formulation, shall we?<\/p>\n<p><strong>Problem<\/strong>: Given a list or a collection of sets. How to <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\">join <\/a>those sets using the intersection operation?<\/p>\n<p><strong>Example<\/strong>: You&#8217;ve got a list of sets <code>[{1, 2, 3}, {1, 4}, {2, 3, 5}]<\/code> and you want to calculate the intersection <code>{1}<\/code>. <\/p>\n<p><strong>Solution<\/strong>: To intersect a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-to-set\/\" target=\"_blank\">list of sets<\/a>, use the following strategy:<\/p>\n<ul>\n<li>Get the first element from the list as a starting point. This assumes the list has at least one element.<\/li>\n<li>Call the <code>intersection()<\/code> method on the first set object.<\/li>\n<li>Pass all sets as arguments into the <code>intersection()<\/code> method by unpacking the list with the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/what-is-asterisk-in-python\/\" target=\"_blank\">asterisk operator<\/a> <code>*list<\/code>.<\/li>\n<li>The result of the <code>intersection()<\/code> method is a new set containing all elements that are in all of the sets.<\/li>\n<\/ul>\n<p><strong>Code<\/strong>: Here&#8217;s the <a rel=\"noreferrer noopener\" href=\"https:\/\/pythononeliners.com\/\" target=\"_blank\">one-liner code <\/a>that intersects a collection of sets.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Create the list of sets\nlst = [{1, 2, 3}, {1, 4}, {1, 2, 3}] # One-Liner to intersect a list of sets\nprint(lst[0].intersection(*lst))<\/pre>\n<p>The output of this code is the intersection of the three sets <code>{1, 2, 3}<\/code>, <code>{1, 4}<\/code>, and <code>{2, 3, 5}<\/code>. Only one element appears in all three sets:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{1}<\/pre>\n<p>If you <strong><em>love Python one-liners<\/em><\/strong>, check out my new book &#8220;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\">Python One-Liners<\/a>&#8221; (Amazon Link) that teaches you a <strong><em>thorough understanding of all single lines of Python code. <\/em><\/strong><\/p>\n<p><strong>Try it yourself<\/strong>: Here&#8217;s the code in an interactive code shell that runs it in your browser:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/5eb34b02c7\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Change the code to calculate the union of the sets in the list!<\/em><\/p>\n<p><strong>Related video<\/strong>: A similar problem is to <a href=\"https:\/\/blog.finxter.com\/union-multiple-sets-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">perform the union operation<\/a> on a list of sets. In the following video, you can watch me explain how to union multiple sets in Python:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Union Multiple Sets in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/0k_Vg_kj4WI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To intersect multiple sets, stored in a list l, use the Python one-liner l.pop().intersection(*l) that takes the first set from the list, calls the intersection() method on it, and passes the remaining sets as arguments by unpacking them from the list. A set is a unique collection of unordered elements. The intersection operation creates a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-113808","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=113808"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113808\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}