Sick Gaming
[Tut] How to Intersect Multiple Sets in Python? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Intersect Multiple Sets in Python? (/thread-95589.html)



[Tut] How to Intersect Multiple Sets in Python? - xSicKxBot - 06-09-2020

How to Intersect Multiple Sets in Python?

<div><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>
<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>
<figure class="wp-block-image size-large is-resized"><img 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="(max-width: 768px) 100vw, 768px" /></figure>
<p>So, let’s dive into the formal problem formulation, shall we?</p>
<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>
<p><strong>Example</strong>: You’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>
<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>
<ul>
<li>Get the first element from the list as a starting point. This assumes the list has at least one element.</li>
<li>Call the <code>intersection()</code> method on the first set object.</li>
<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>
<li>The result of the <code>intersection()</code> method is a new set containing all elements that are in all of the sets.</li>
</ul>
<p><strong>Code</strong>: Here’s the <a rel="noreferrer noopener" href="https://pythononeliners.com/" target="_blank">one-liner code </a>that intersects a collection of sets.</p>
<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
lst = [{1, 2, 3}, {1, 4}, {1, 2, 3}] # One-Liner to intersect a list of sets
print(lst[0].intersection(*lst))</pre>
<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>
<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>
<p>If you <strong><em>love Python one-liners</em></strong>, check out my new book “<a rel="noreferrer noopener" href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank">Python One-Liners</a>” (Amazon Link) that teaches you a <strong><em>thorough understanding of all single lines of Python code. </em></strong></p>
<p><strong>Try it yourself</strong>: Here’s the code in an interactive code shell that runs it in your browser:</p>
<p> <iframe src="https://trinket.io/embed/python/5eb34b02c7" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Change the code to calculate the union of the sets in the list!</em></p>
<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>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/06/05/how-to-intersect-multiple-sets-in-python/