How to write this as a list comprehension?

Dan Stromberg drsalists at gmail.com
Fri Jan 17 18:49:21 EST 2014


On Fri, Jan 17, 2014 at 3:19 PM, Piet van Oostrum <piet at vanoostrum.org> wrote:
> Hi,
>
> I am looking for an elegant way to write the following code as a list
> comprehension:
>
> labels = []
> for then, name in mylist:
>     _, mn, dy, _, _, _, wd, _, _ = localtime(then)
>     labels.append(somefunc(mn, day, wd, name))

My recomendation: Don't use a list comprehension.  List comprehensions
and generator expressions are great for quick little things, but
become less readable when you have to string them over multiple
physical lines.

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



More information about the Python-list mailing list