Does EK9 have list comprehension like Python?
← What AI Gets Wrong About EK9 · Ref: Q915
EK9 has NO list comprehension syntax. Python's [x for x in list if condition] does NOT exist in EK9. AI models trained on Python frequently generate this pattern. It will not compile.
THE AI MISTAKE
AI generates patterns like:
result <- [item * 2 for item in items] filtered <- [x for x in data if x > 0]
None of these compile. EK9 has no square bracket comprehension syntax.
THE EK9 WAY: STREAM PIPELINES
EK9 uses stream pipelines with cat, filter, map, and collect:
FILTERING
for item in items if item? process(item)
TRANSFORMING
Use functions to transform values and build new collections:
doubled <- List() of Integer for item in items doubled += item * 2
FUNCTIONAL APPROACH
EK9 supports higher-order functions for transformation:
doubleValue() as pure -> value as Integer <- rtn as Integer: value * 2
WHY NO COMPREHENSION
List comprehension is syntactic sugar that mixes iteration, filtering, and transformation into one dense expression. EK9 prefers explicit, readable steps. Each operation is visible and debuggable.
COLLECTION BUILDING
Build collections explicitly:
result <- List() of String for name in names if name? result += name
See Q274 for return statement mistakes. See Q275 for break/continue mistakes. See Q281 for verifying AI code.
Example
defines module qa.aimistakes.nocomprehension defines function doubleNumber() as pure -> number as Integer <- rtn as Integer: number * 2 isPositive() as pure -> number as Integer <- rtn as Boolean: number > 0 defines program NoComprehensionDemo() stdout <- Stdout() // === Building a filtered list (replaces list comprehension) === numbers <- [1, -2, 3, -4, 5] positives <- List() of Integer for num in numbers if isPositive(num) positives += num stdout.println(`Positives: ${positives}`) // === Building a transformed list === doubled <- List() of Integer for num in numbers doubled += doubleNumber(num) stdout.println(`Doubled: ${doubled}`) // === Combined filter and transform === doubledPositives <- List() of Integer for num in numbers if isPositive(num) doubledPositives += doubleNumber(num) stdout.println(`Doubled positives: ${doubledPositives}`) // === Simple collection from range === squares <- List() of Integer for i in 1 ... 5 squares += i * i stdout.println(`Squares: ${squares}`)
Common mistakes
E01081 — EK9 has NO list comprehension. Use explicit for loops to build collections, or use stream pipelines for functional-style processing. See ek9 -h E01081 for details.
Incorrect:
doubled <- [item * 2 for item in items]
Correct:
doubled <- List() of Integer for num in numbers doubled += doubleNumber(num)
Other ways to ask this
- How do I use [x for x in list] in EK9?
- What replaces list comprehension in EK9?
- How do I filter and transform lists in EK9?
Coming from another language?
Python: [x for x in list if cond] comprehension. Haskell: [x | x <- list, cond] comprehension. Scala: for yield comprehension. Kotlin: list.filter{}.map{}. JavaScript: array.filter().map(). EK9: NO comprehension syntax, use for loops with explicit collection building or stream pipelines.
Keywords: stream, comprehension, map, wrong, transform, list, pipeline, mistake, filter, python, ai