Python Comprehensions & Generators Made Easy (PyCharm)
Tags: python, productivity, codequality, tutorial
Published: 2020-07-11 06:53:02

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. 

In this brief blog, show how to use them in PyCharm (works for both Community or Professional editions)

# Python Comprehensions & Generators 101

Comprehensions and generators can help write concise yet expressive code. Consider this loop: 

```
small_words = []
for word in quip.split(" "):
    if(len(word.upper()) <= 4):
        small_words.append(word.upper())
print(small_words)
``` 
This is quite verbose. 

Here is how it could be expressed with a list comprehension - which is quite readable as well: 

```
small_words = [word.upper() for word in quip.split(" ") if len(word) <= 4]
print(small_words)
```

Here are the parts of the comprehension: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aguwesnh5g5zf1ul7dud.png) 

That's easy and nice, isn't it! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/frp9e02tan2rrygq6eop.gif)

# Live Templates in PyCharm 

You can use Live Templates - the template code segments that you can fill - in PyCharm. 

Here is the list of live templates. You can access it as `PyCharm -> Preferences -> Editor -> Live Templates`.  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/so1ssl6azmtp6o4l1fxp.png)

# Getting Started - Easy and Simple Live Templates

The live templates `main`, `iter` and `itere` are easy and simple ones to get started with. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pdhvwxqu5m6ppy4lmr7b.png)

In live action in this YouTube Video. 

{% youtube qtPTPAIvG4s %} 

It's repetition made easy! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d4e1ktyhwi99qiayeooc.gif)

# Templates for List, Set, and Dictionary Comprehensions 

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

So goes `comps`, `compsi`, `compd` and `compdi` for set and dictionary comprehensions with optional if conditions. 

## List comprehension

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/370j35p9p8gy4mgsmm66.png)
  
That's how you process lists! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/17ydyca43027bqd7m2p5.gif)

## Set comprehension

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jp14z0kl4i2rynr1abwy.png)
  
That's how you deal with sets! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3tc6spxigfaxptsrhw16.gif)

## Dictionary comprehension

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ex97w4la91jht6wsj1av.png)
  
That's how you deal with dictionaries! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ty0p5dph4j1f193i5c2k.gif)

## Generator comprehension

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 - `<generator object <genexpr> at 0x102c1b7c8>`. So convert to list, for example, before printing, like `print(list(small_words))`. 

Here is an example. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ie0266sdne8b9kaicfhf.png)

That's how you deal with generators! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cdrr6ksbp3hjb6x87pm6.gif)

## Live Templates to Remember 

* `main` - __main__ check
* `iter` / `itere` - for loop 
* `compl` / `compli` - list comprehension
* `comps` / `compsi` - set comprehension
* `compd` / `compdi` - dictionary  comprehension
* `compg` / `compgi` - generators 

Have fun playing around with comprehensions and generators in PyCharm. Check out for ~1 min [PyCharm Tips & Tricks here] (www.youtube.com/c/CodeOpsTech). 

Have fun coding! 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l23eisjrgrkp40ptqtgu.gif)