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 and set comprehensions follow similar patterns.

Generator expressions look pretty much like comprehensions and supports 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:

Python Syntax
[expression for item in iterable if condition]

Here’s an example:

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:

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

Here’s a quick example:

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:

Python Syntax
{expression for item in iterable if condition}

Example:

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

Generator Expression

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

The syntax is the following:

Python Syntax
(expression for item in iterable if condition)

Here’s an example:

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

    Python
    # x**2 is the expression here
    [x**2 for x in range(5)]
    

  • Iterator variable: The variable used in the loop

    Python
    # x is the iterator variable here
    [x**2 for x in range(5)]
    

  • Iterable: The sequence being iterated over

    Python
    # range(5) is the iterable here
    [x**2 for x in range(5)]
    

  • Conditional (Optional): A condition used to filter items

    Python
    # if x > 5 is the conditional here
    [x**2 for x in range(10) if x > 5]
    

Nested Comprehensions

Comprehensions can be nested for more complex operations:

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:

    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:

    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:

    Python
    >>> words = ['hello', 'world', 'python']
    >>> titles = [word.title() for word in words]
    >>> titles
    ['Hello', 'World', 'Python']
    

Tutorial

When to Use a List Comprehension in Python

Python list comprehensions help you to create lists while performing sophisticated filtering, mapping, and conditional logic on their members. In this tutorial, you'll learn when to use a list comprehension in Python and how to create them effectively.

basics python

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


By Dan Bader • Updated April 11, 2025 • Reviewed by Leodanis Pozo Ramos