Style in list comprehensions
Carl Banks
imbosol at aerojockey.com
Fri Aug 15 16:08:11 EDT 2003
Tim Lesher wrote:
> 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,
I disagree. I suspect most Pythonistas would disagree, too.
> but do I incur any overhead for
> creating the list of results when I'm not going to use it?
Of course you do. You've devoted time and RAM (temporarily) to
allocating a list you'll never use. Only you can decide whether the
overhead is significant to you.
Regardless, just from a stylistic standpoint, I highly discourage
using a list comp only for it's side effects.
--
CARL BANKS
"You don't run Microsoft Windows. Microsoft Windows runs you."
More information about the Python-list
mailing list