<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vatsal Juneja</title>
    <description>The latest articles on DEV Community by Vatsal Juneja (@vatsalj).</description>
    <link>https://dev.to/vatsalj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F417939%2Fa8d4ee82-05bd-4308-963c-3d3369396bf7.jpeg</url>
      <title>DEV Community: Vatsal Juneja</title>
      <link>https://dev.to/vatsalj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vatsalj"/>
    <language>en</language>
    <item>
      <title>Comprehensions in Python: Why are they faster? A deep dive.</title>
      <dc:creator>Vatsal Juneja</dc:creator>
      <pubDate>Sun, 28 Jun 2020 08:04:12 +0000</pubDate>
      <link>https://dev.to/vatsalj/comprehensions-in-python-why-are-they-faster-a-deep-dive-1pij</link>
      <guid>https://dev.to/vatsalj/comprehensions-in-python-why-are-they-faster-a-deep-dive-1pij</guid>
      <description>&lt;p&gt;I've asked this question to Python developers - beginners and advanced Python devs alike. Their answer, however, more often than not doesn't answer the fundamental question: are they the same as a simple for loop.&lt;/p&gt;

&lt;p&gt;The internet does have a lot of articles around how you use them, but they do not cover the &lt;code&gt;why&lt;/code&gt; - why do list comprehensions exist? What did the vanilla for loop miss that we had to create another syntax to do the same thing? I'm sharing a short summary of how you can use them and why are they faster, but if you want to deep dive, feel free to read more at &lt;a href="https://www.angrynerd.in/posts/comprehensions-in-python-the-hard-way/?utm_source=dev.to"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are comprehensions?
&lt;/h1&gt;

&lt;p&gt;In short, comprehensions can help you shorten your code while giving a performance boost to your code. As an example, if you want to loop over a range of integers &amp;amp; add 1 to all, the for loop will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;la_ints&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;la_ints&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, a comprehension is much shorter and ~30% faster&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;la_ints&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  But.. why?
&lt;/h1&gt;

&lt;p&gt;You might already know that Python itself is built on top of C - which is much faster than Python when it comes to handling raw data. When you're using a list comprehension, the evaluation logic of the loop is taken care of by the lower level implementation which is much faster. If you see the disassembly of these implementation using &lt;code&gt;dis&lt;/code&gt; module, you'll see how append kills the performance of the for loop.&lt;/p&gt;

&lt;p&gt;In my blog post &lt;a href="https://www.angrynerd.in/posts/comprehensions-in-python-the-hard-way/?utm_source=dev.to"&gt;Comprehensions in Python: The hard way&lt;/a&gt;, I deep dive into disassembling of the two implementations (including sweet sweet bytecodes), and how to manage memory with generator expressions when you're hitting memory limits.&lt;/p&gt;

&lt;p&gt;Happy to hear any feedback about this post of the blog post! Thanks!&lt;/p&gt;

</description>
      <category>python</category>
      <category>computerscience</category>
      <category>c</category>
    </item>
  </channel>
</rss>
