Style in list comprehensions

Tim Lesher tim at lesher.ws
Sat Aug 16 14:11:38 EDT 2003


"Tim Peters" <tim.one at comcast.net> wrote in message news:<mailman.1060976550.7996.python-list at python.org>...
> [Tim Lesher]
> > 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,
> 
> Your Pythonic intuition needs refinement, then <wink>.  

That's what I was afraid of.  I couldn't really come up with a reason
that it seemed better, which is why I posted.  While Pythonicity
generally leads to shorter code, I understand that the reverse isn't
true.

> > but do I incur any overhead for creating the list of results when I'm
> > not going to use it?
> 
> Of course, but avoiding overhead isn't the best reason for preferring the
> first way.

Unclear posting on my part.  What I really meant was "why should I
incur overhead", not "do I incur overhead".

Consider me corrected. I shall go write 500 lines of "Explicit is
better than implicit" now.

-- 
Tim Lesher
tim at lesher.ws




More information about the Python-list mailing list