<?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: CodeOps Technologies</title>
    <description>The latest articles on DEV Community by CodeOps Technologies (@codeopstech).</description>
    <link>https://dev.to/codeopstech</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F429083%2F0c0438fb-1950-4090-ab60-90502d752b2d.jpg</url>
      <title>DEV Community: CodeOps Technologies</title>
      <link>https://dev.to/codeopstech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeopstech"/>
    <language>en</language>
    <item>
      <title>Python Comprehensions &amp; Generators Made Easy (PyCharm)</title>
      <dc:creator>CodeOps Technologies</dc:creator>
      <pubDate>Sat, 11 Jul 2020 07:00:50 +0000</pubDate>
      <link>https://dev.to/codeopstech/python-comprehensions-generators-made-easy-pycharm-3889</link>
      <guid>https://dev.to/codeopstech/python-comprehensions-generators-made-easy-pycharm-3889</guid>
      <description>&lt;p&gt;One of the features I love is Live Templates in PyCharm: With it, you can code super fast! They are incredibly useful for speeding up common tasks such as looping over (or generating from) a collection object. &lt;/p&gt;

&lt;p&gt;In this brief blog, show how to use them in PyCharm (works for both Community or Professional editions)&lt;/p&gt;

&lt;h1&gt;
  
  
  Python Comprehensions &amp;amp; Generators 101
&lt;/h1&gt;

&lt;p&gt;Comprehensions and generators can help write concise yet expressive code. Consider this loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;small_words = []
for word in quip.split(" "):
    if(len(word.upper()) &amp;lt;= 4):
        small_words.append(word.upper())
print(small_words)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is quite verbose. &lt;/p&gt;

&lt;p&gt;Here is how it could be expressed with a list comprehension - which is quite readable as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;small_words = [word.upper() for word in quip.split(" ") if len(word) &amp;lt;= 4]
print(small_words)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here are the parts of the comprehension: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iOE6nwn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aguwesnh5g5zf1ul7dud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iOE6nwn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aguwesnh5g5zf1ul7dud.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;That's easy and nice, isn't it! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8NuTtSCd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/frp9e02tan2rrygq6eop.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8NuTtSCd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/frp9e02tan2rrygq6eop.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Live Templates in PyCharm
&lt;/h1&gt;

&lt;p&gt;You can use Live Templates - the template code segments that you can fill - in PyCharm. &lt;/p&gt;

&lt;p&gt;Here is the list of live templates. You can access it as &lt;code&gt;PyCharm -&amp;gt; Preferences -&amp;gt; Editor -&amp;gt; Live Templates&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0xTsuVxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/so1ssl6azmtp6o4l1fxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0xTsuVxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/so1ssl6azmtp6o4l1fxp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started - Easy and Simple Live Templates
&lt;/h1&gt;

&lt;p&gt;The live templates &lt;code&gt;main&lt;/code&gt;, &lt;code&gt;iter&lt;/code&gt; and &lt;code&gt;itere&lt;/code&gt; are easy and simple ones to get started with. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bY_Kkq7C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pdhvwxqu5m6ppy4lmr7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bY_Kkq7C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pdhvwxqu5m6ppy4lmr7b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In live action in this YouTube Video. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qtPTPAIvG4s"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;p&gt;It's repetition made easy! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HSFviuQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d4e1ktyhwi99qiayeooc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HSFviuQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d4e1ktyhwi99qiayeooc.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Templates for List, Set, and Dictionary Comprehensions
&lt;/h1&gt;

&lt;p&gt;This is where the fun begins - let it be list, set or dictionaries, comprehensions are easy with PyCharm. Just start typing &lt;code&gt;comp&lt;/code&gt; which stands for 'comprehension'. For list comprehension, type &lt;code&gt;l&lt;/code&gt;, so it's &lt;code&gt;compl&lt;/code&gt;. For adding an if condition, add an &lt;code&gt;i&lt;/code&gt;, so it becomes &lt;code&gt;compli'. Nothing&lt;/code&gt;compli`cated! &lt;/p&gt;

&lt;p&gt;So goes &lt;code&gt;comps&lt;/code&gt;, &lt;code&gt;compsi&lt;/code&gt;, &lt;code&gt;compd&lt;/code&gt; and &lt;code&gt;compdi&lt;/code&gt; for set and dictionary comprehensions with optional if conditions. &lt;/p&gt;

&lt;h2&gt;
  
  
  List comprehension
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GnL88HKm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/370j35p9p8gy4mgsmm66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GnL88HKm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/370j35p9p8gy4mgsmm66.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's how you process lists! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VEjlgQHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/17ydyca43027bqd7m2p5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VEjlgQHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/17ydyca43027bqd7m2p5.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Set comprehension
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3g1QjmuA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jp14z0kl4i2rynr1abwy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3g1QjmuA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jp14z0kl4i2rynr1abwy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's how you deal with sets! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GHu73Kxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3tc6spxigfaxptsrhw16.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GHu73Kxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3tc6spxigfaxptsrhw16.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary comprehension
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R7hjkhtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ex97w4la91jht6wsj1av.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R7hjkhtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ex97w4la91jht6wsj1av.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's how you deal with dictionaries! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5UgS5zPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ty0p5dph4j1f193i5c2k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5UgS5zPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ty0p5dph4j1f193i5c2k.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Generator comprehension
&lt;/h2&gt;

&lt;p&gt;Generators create a sequence - we iterate over them one at a time. If you try to print a generator you'll get a generator object (not its content printed!). For example, if small_words is a generator, then print(small_words) will print cryptic reading one like - &lt;code&gt;&amp;lt;generator object &amp;lt;genexpr&amp;gt; at 0x102c1b7c8&amp;gt;&lt;/code&gt;. So convert to list, for example, before printing, like &lt;code&gt;print(list(small_words))&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Here is an example. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TpGtqR8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ie0266sdne8b9kaicfhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TpGtqR8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ie0266sdne8b9kaicfhf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's how you deal with generators! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CVRGeif6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cdrr6ksbp3hjb6x87pm6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CVRGeif6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cdrr6ksbp3hjb6x87pm6.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Templates to Remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; - &lt;strong&gt;main&lt;/strong&gt; check&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;iter&lt;/code&gt; / &lt;code&gt;itere&lt;/code&gt; - for loop &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;compl&lt;/code&gt; / &lt;code&gt;compli&lt;/code&gt; - list comprehension&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;comps&lt;/code&gt; / &lt;code&gt;compsi&lt;/code&gt; - set comprehension&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;compd&lt;/code&gt; / &lt;code&gt;compdi&lt;/code&gt; - dictionary  comprehension&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;compg&lt;/code&gt; / &lt;code&gt;compgi&lt;/code&gt; - generators &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have fun playing around with comprehensions and generators in PyCharm. Check out for ~1 min &lt;a href="//www.youtube.com/c/CodeOpsTech"&gt;PyCharm Tips &amp;amp; Tricks here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Have fun coding! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tmb51AqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l23eisjrgrkp40ptqtgu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tmb51AqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l23eisjrgrkp40ptqtgu.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
