Can this be done with list comprehension?

Maric Michaud maric at aristote.info
Mon Jun 9 18:32:04 CEST 2008


Le Sunday 08 June 2008 02:27:54 Terry Reedy, vous avez écrit :
> "Karlo Lozovina" <_karlo_ at _mosor.net_> wrote in message
> news:Xns9AB7858EC056mosornet at 161.53.160.64...
>
> | I figured that out few minutes ago, such a newbie mistake :). The fix I
> | came up with is:
> |
> |  result = ['something'] + [someMethod(i) for i in some_list]
> |
> | Are there any other alternatives to this approach?
>
> result = [something]
> result.extend(someMethod(i) for i in some_list)
>
> avoids creating and deleting an intermediate list
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

A one liner, though it's a bit lispy :

list(itertools.chain((something,), (someMethod(i) for i in some_list)))



-- 
_____________

Maric Michaud



More information about the Python-list mailing list