Skip to content
Rochester, NY design, systems, study tools, and experiments Built in public

Python list comprehensions

A concise-list-building syntax that replaces simple for-loop-and-append patterns. Supports filtering via an inline if clause.

python python syntax iterables 2026-04-15

List comprehensions offer a concise syntax for creating lists in Python. The basic structure is [expression for item in iterable], which is equivalent to initializing an empty list and appending results within a for loop.

Filtering is achieved by adding an if clause: [expression for item in iterable if condition]. This restricts the items processed to only those meeting the specified condition.

Use comprehensions when the logic is straightforward; they significantly improve readability over explicit for loops for simple transformations or filtering. If the logic becomes complex (e.g., multiple nested loops or extensive conditional branching), a traditional for loop may be clearer.

Example:

squares = [x*x for x in range(5)]
evens_sq = [x*x for x in range(10) if x % 2 == 0]