Language Syntax

List comprehension — Python syntax

Construct a list eagerly by evaluating the expression for selected items. Multiple for and if clauses follow nested-loop order; iteration variables are local to the comprehension.

Formal Syntax & Structure

python syntaxpython
[expression for target in iterable if condition]

Parameters & Attributes

NameTypeDescription
expressionexpressionValue to append for selected items.
targetassignment targetComprehension-local iteration target.
iterableexpressionSource of items.
conditionexpressionOptional filter.

Return Value

list

Value obtained by evaluating this expression; see description for context and constraints.

Working Example

Example Snippetpython
x=99result=[x*x for x in range(5) if x%2]print((result,x))
Python Reference