Endorsement of list comprehensions

Alex Martelli aleax at aleax.it
Thu May 1 18:28:42 EDT 2003


Colin J. Williams wrote:
   ...
> You wrote:
>    [ func_using_var for var in some_list if condition_using_var ]
> 
> It might be slightly more generally be expressed as:
>    [expr_using_var for var in some_list if condition_using_var ]

And even more generally you could have expr_POTENTIALLY_using_var.

An important idiom where the expr doesn't use the var:

    [[0]*M for i in range(N)]

this answers the FAQ of how to build an N x M 'matrix' (list of
_independent and separate_ sublists).


Alex





More information about the Python-list mailing list