Skip to content

comprehension

Python comprehensions are concise syntax patterns for creating collections like lists, dictionaries, and sets in a single line of code, offering a readable alternative to traditional loops.

List comprehensions, the most common type, allow you to transform and filter data in a clear, expressive syntax. Dictionary comprehensions and set comprehensions follow similar patterns.

Generator expressions look pretty much like comprehensions and support memory-efficient iteration by generating values on demand.

List Comprehension

The most common form of comprehension in Python, used to create lists in a concise way. The basic syntax is:

Language: Python Syntax
[expression for item in iterable if condition]

Here’s an example:

Language: Python
>>> [x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Dictionary Comprehension

Creates dictionaries using a similar syntax to list comprehensions:

Language: Python Syntax
{key_expression: value_expression for item in iterable if condition}

Here’s a quick example:

Language: Python
>>> {x: x**2 for x in range(5)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Set Comprehension

Creates sets using the comprehension syntax with curly braces:

Language: Python Syntax
{expression for item in iterable if condition}

Example:

Language: Python
>>> {x**2 for x in range(10) if x % 2 == 0}
{0, 64, 4, 36, 16}

Generator Expression

Generator expressions use a syntax similar to list comprehension but with enclosing parentheses rather than square brackets. They allow you to create a generator object that yields values on demand, which makes them pretty efficient when you need to iterate over large datasets.

The syntax is the following:

Language: Python Syntax
(expression for item in iterable if condition)

Here’s an example:

Language: Python
>>> gen = (x**2 for x in range(5))

>>> gen
<generator object <genexpr> at 0x111d12e90>

>>> for item in gen:
...     print(item)
...
0
1
4
9
16

Key Components

Expression: The operation or value to be included in the final collection:

Language: Python
# The expression is x**2
[x**2 for x in range(5)]

Loop variable: The variable used in the loop:

Language: Python
# The loop variable is x
[x**2 for x in range(5)]

Iterable: The sequence being iterated over:

Language: Python
# The iterable is range(5)
[x**2 for x in range(5)]

Conditional (optional): A condition used to filter items:

Language: Python
# The conditional is if x > 5
[x**2 for x in range(10) if x > 5]

Nested Comprehensions

Comprehensions can be nested for more complex operations:

Language: Python
>>> [[i+j for j in range(3)] for i in range(3)]
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Common Use Cases

Data transformation:

Language: Python
>>> temperatures_f = [32, 68, 95]
>>> temperatures_c = [(f - 32) * 5/9 for f in temperatures_f]
>>> temperatures_c
[0.0, 20.0, 35.0]

Filtering data:

Language: Python
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> evens = [x for x in numbers if x % 2 == 0]
>>> evens
[2, 4, 6, 8, 10]

String manipulation:

Language: Python
>>> words = ["hello", "world", "python"]
>>> titles = [word.title() for word in words]
>>> titles
['Hello', 'World', 'Python']
When to Use a List Comprehension in Python

Tutorial

Python List Comprehension: Tutorial With Examples

Learn Python list comprehensions with clear examples. Create, filter, and transform lists using concise, readable one-line expressions.

basics python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Dan Bader • Updated Sept. 25, 2026 • Reviewed by Leodanis Pozo Ramos