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
| Name | Type | Description |
|---|---|---|
| expression | expression | Value to append for selected items. |
| target | assignment target | Comprehension-local iteration target. |
| iterable | expression | Source of items. |
| condition | expression | Optional 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))