Style in list comprehensions

Michael Peuser mpeuser at web.de
Fri Aug 15 15:44:28 EDT 2003


"Tim Lesher" <tim at lesher.ws> schrieb im Newsbeitrag
news:b6562547.0308151116.15dd7591 at posting.google.com...
> Suppose I have a list of objects and I want to call a method on each.
> I can do the simple:
>
> for i in objs:
>     i.meth(arg1, arg2)
>
> or using list comprehensions:
>
> [i.meth(arg1, arg2) for i in objs]
>
> The second feels more Pythonic, but do I incur any overhead for
> creating the list of results when I'm not going to use it?

Why do you think that to be more "pythonic"? To my understanding list
comprehensions have been invented as a more understandable MAP/GREP notation
(for... if...).

If you do not use the results as a list I see little advantage. Of course
there can be a more effecient execution, but the contrary can as well be the
case, and this is probably version dependent.

Why don't you do a benchmark???

Kindly
Michael P


>
> --
> Tim Lesher
> tim at lesher.ws






More information about the Python-list mailing list