PyCon Pittsburgh. April 15-23, 2020.

Talk: Comprehensible Comprehensions

Presented by:

Trey Hunner


Finding list comprehensions incomprehensible? Having trouble figuring out when to use list comprehensions or just plain for loops? Are your coworkers overusing comprehensions?

Come to this talk and learn the how, when, and why of list comprehensions.

We’ll discuss:

  • why list comprehensions exist and why people use them (yes you can always use a loop instead)
  • how to use list, set, an dictionary comprehensions as well as generator expressions
  • when and how to easily turn a for loop into a comprehension
  • why list comprehensions are often hard to read and how to make your comprehensions readable
  • When to use generator expressions instead of list comprehensions
  • when and how not to use comprehensions (they’re not a replacement for all loops)

Comprehensions are a unique tool for a unique task. Even if you dislike them, understanding how they work will help you better understand the Python code you find in the wild.

You’ll leave this talk with a better appreciation for both the use cases for comprehensions in Python and an understand of when not to use them.


Watch on YouTube