How to write this as a list comprehension?

Jussi Piitulainen jpiitula at ling.helsinki.fi
Sat Jan 18 11:00:45 EST 2014


Rustom Mody writes:

> On Saturday, January 18, 2014 2:06:29 PM UTC+5:30, Peter Otten wrote:
>
> > What would a list-comp with `let` or `where` look like? Would it
> > win the beauty contest against the loop?
> 
> For me this is neat
> 
> [somefunc(mn,day,wd,name) for (then, name) in mylist let (_,mn,dy,_,_,_,wd,_,_) = localtime(then)]
>    
> Others may not find it so!
> 
> See it across > 1 line (as I guess it will come after being posted!)
> and its not so neat.

I would write that on three lines anyway, properly indented:

  [ somefunc(mn,day,wd,name)
    for (then, name) in mylist
    let (_,mn,dy,_,_,_,wd,_,_) = localtime(then) ]

It could be made to use existing keywords:

  [ somefunc(mn,day,wd,name)
    for (then, name) in mylist
    with localtime(then) as (_,mn,dy,_,_,_,wd,_,_) ]



More information about the Python-list mailing list