loops -> list/generator comprehensions

Alex Martelli aleaxit at yahoo.com
Sun Feb 6 17:36:07 EST 2005


Steven Bethard <steven.bethard at gmail.com> wrote:

> at the OP's original code, the line:
> 
>  [(x[0], x[2]) for x in os.walk(".")]
> 
> is the equivalent of:
> 
>  [dirpath, filenames for dirpath, dirnames, filenames in os.walk('.')]

Just a nit: you need parentheses in your second LC too, i.e.:

 [(dirpath, filenames) for dirpath, dirnames, filenames in os.walk('.')]

> I prefer to give names to the values produced by os.walk -- I think it
> makes the usage much clearer.

Full agreement here -- those numeric indices are nasty.


Alex



More information about the Python-list mailing list