QamoosTech
Programming FundamentalsIteration and generatorsBeginnerPython

Comprehension

Pronunciationkom-pri-HEN-shun

Definition

A comprehension is a short expression that builds a list, dictionary or set by looping over something and optionally filtering it.

Where you hear it

In Python code reviews and tutorials, whenever a small loop that only builds a collection can be shortened.

Examples

  • [n * n for n in numbers if n > 0] builds the squares of the positive numbers.
  • A dictionary comprehension turns the list of pairs into a lookup table.
  • This comprehension keeps only the active users and collects their emails.

Common mistake

Cramming complex logic into one line. If it needs more than one condition or is hard to read, use a normal loop.

Don't confuse with

A generator expression, which looks the same but with parentheses and produces items lazily.

Say it at work

  • In a meeting or chatThat loop is just building a list; turn it into a comprehension.
  • In an email or ticketThis comprehension is too dense, so let's expand it.