RUNTIME VALUE CONVERSIONS

List, dictionary, and set comprehensions

Build lists, dictionaries, and sets from supported iterables with multiple generators, filters, and unpacking. These forms use runtime value conversion.

Transform and filter values

def even_squares(limit: int) -> list[int]:
    return [value * value for value in range(limit) if value % 2 == 0]

even_squares(6) returns [0, 4, 16]. The result expression runs only for elements that pass every filter. An empty iterable produces an empty list.

Multiple generators and dependent iterables

def pairs(limit: int) -> list[tuple[int, int]]:
    return [(left, right)
            for left in range(limit)
            for right in range(left)
            if right % 2 == 0]

pairs(4) returns [(1, 0), (2, 0), (3, 0), (3, 2)]. Generators run in written order like nested loops. Inner iterables may depend on earlier target values.

Unpack elements and apply multiple filters

def select_rows(rows: list[tuple[str, int]]) -> list[str]:
    return [name + ": " + str(count)
            for name, count in rows
            if count > 0
            if name != ""]

For [("apple", 2), ("", 3), ("pear", 0)], the result is ["apple: 2"]. Use a fixed target shape with a matching element count, or one starred target at each nesting level to collect the remaining elements into a list.

Comprehension scope and order

def main() -> int:
    value = 99
    selected = [value for value in range(4) if value > 1]
    print(value, selected[0], selected[1])
    return 0

The output is 99 2 3. Comprehension targets have a separate scope and do not replace the surrounding local variable. Expressions and filters preserve their written evaluation order. Lambdas created inside a comprehension capture its live target cells; see Late binding.

Dictionary and set results

def positive_counts(counts: dict[str, int]) -> dict[str, int]:
    return {name: count for name, count in counts.items() if count > 0}

Dictionary comprehensions support the same filters, dependent iterables, nested generators, and local target scope. For each admitted item, the key expression runs before the value expression, then the pair is inserted. Repeated keys replace the value while keeping the first insertion position.

A set comprehension such as {value ** 2 for value in values} follows the same scope and evaluation rules. Its elements must be hashable; equal results appear once.

Supported iterables and results

Iterate over strings, lists, tuples, ranges, dictionaries, sets, frozen sets, live dictionary views, admitted iterators, and readable files. Strings produce one-character strings. Results may contain mixed values, nested lists/tuples, class instances, or lambdas, subject to any declared result annotation.

List, dictionary, and set comprehensions allocate their result in the C context. Result size, nested iterations, and annotation checks consume the same memory and work budgets as ordinary runtime operations. Generator expressions, async generators, and arbitrary custom iterators are unsupported.