Make your comprehensions readable and be careful not to overuse them

Trey Hunner3 min read
Hey!

List comprehensions are one of my favorite Python features.

List comprehensions are a special-purpose tool for building new lists out of old lists (or any iterable really). Comprehensions often overlooked by new Python programmers and sometimes abused by long-time Python programmers.

This email contains:
  1. Resources for folks new to comprehensions
  2. Advice for folks experienced with comprehensions
  3. A warning about overusing comprehensions
  4. Tips for getting practice with comprehensions

New(ish) to comprehensions?

List comprehensions look like this:
squares = [n**2 for n in numbers]
List comprehensions are largely about improving code readability (yes they do seem hard to read at first): they help experienced Python programmers quickly notice at a glance that the code they're looking at builds a new list out of an old list (as opposed to "for" loops, which can also use looping for some other end)

Never seen a list comprehension before? Read (or watch) What are list comprehensions? (3 minutes)

Seen list comprehensions but struggle when writing your own? Read (or watch) Turning a for loop into a list comprehension (5 minutes)

If you prefer longer articles or talks either:
  1. Read my article explaining list comprehensions visually (6 minutes)
  2. Watch my Comprehensible Comprehensions (30 minutes)

When writing comprehensions, whitespace is your friend

I find that my list comprehensions are often more readable at a glance when I break my comprehensions up over multiple lines.

Instead of this:
squared_odds = [n**2 for n in numbers if n % 2 == 1]
I prefer to break up each component onto its own line, like this:
squared_odds = [
    n**2
    for n in numbers
    if n % 2 == 1
]
Your code will take up more space this way, but that doesn't matter. Readability is often more important than vertical space.

Long-time Python programmers sometimes overlook this advice. And code auto-formatters (unfortunately) sometimes collapse multi-line comprehension code onto a single-line even when the multi-line version looks more readable.

If you're not already a multi-line comprehension writer, please try it out and see what you think. You might find that it's a habit you'd like to adopt in your own code.

Don't abuse comprehensions

Comprehensions are only for making new lists. While you could use them for printing, you really shouldn't. And sometimes comprehensions simply aren't the most succinct tool for the job (don't forget about Python's list constructor).

Regardless of whether you're new to comprehensions or experienced with them, if you're a fan I recommend skimming my article on overusing comprehensions. That article inspired a pylint warning as well. If you're not using pylint to check your code already, I recommend trying it out (it's quite opinionated by default though).

Get some practice

We learn by trying to retrieve information from our heads, not by putting information into our heads. To solidify your learning, you need to practice this skill by writing some code!

Take a look at your own code and see whether you can spot "for" loops that could be copy-pasted into comprehensions (I mention my copy-pasting recipe in most of the videos, articles, talks, and tutorials above). Spotting code that could be a comprehension is the first step to using them.

Want to jump-start your comprehension habit? I've bundled up my 6 favorite comprehension-related exercises in the Python Morsels Comprehension Path. I recommend 3-6 hours to get through those exercises because each includes a detailed solution walk through after each challenge (like a code review, but in reverse). You can get access to that learning path for just $35 or if you're a Python Morsels subscriber you can access it for free as part of your subscription.

Next week I'll send an email about why we use so many looping helpers in Python.
Postal address: 12651 Monterey Cypress Way, San Diego, CA 92130
8 links in this issue