DEV Community

Cover image for Python Comprehensions & Generators Made Easy (PyCharm)
CodeOps Technologies
CodeOps Technologies

Posted on

Python Comprehensions & Generators Made Easy (PyCharm)

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

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

Alt Text

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

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

In live action in this YouTube Video.

It's repetition made easy!

Alt Text

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'. Nothingcompli`cated!

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

List comprehension

Alt Text

That's how you process lists!

Alt Text

Set comprehension

Alt Text

That's how you deal with sets!

Alt Text

Dictionary comprehension

Alt Text

That's how you deal with dictionaries!

Alt Text

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

That's how you deal with generators!

Alt Text

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.

Have fun coding!

Alt Text

Top comments (0)