A list comprehension constructs a list by applying an expression to elements selected through generators and optional predicates. In Python,
[x*x for x in range(5) if x % 2 == 0] gives [0, 4, 16]. The generator sets the traversal, the predicate filters values and the expression transforms those retained. Nested generators preserve an order, so changing them can change output order and termination for infinite inputs in lazy languages. A Python list comprehension constructs the whole result eagerly; a generator expression instead produces values on demand.[ref]For a binary operation , its Cartesian lifting sends lists to the list comprehension , with the right generator varying fastest. On finite lists this preserves associativity but need not preserve commutativity or idempotence, because list order and repeated entries matter.
Articles by others on the same topic
There are currently no matching articles.