Pythonic way to sum n-th list element?

Oren Tirosh oren-py-l at hishome.net
Sat Apr 19 04:15:55 EDT 2003


On Sat, Apr 19, 2003 at 12:38:06AM +0000, Tyler Eaves wrote:
...
> > Using lists 10 or 100 times as long seems to show O(N) behavior for
> > each case, as could be expected.  So, the simplest (and I would agree
> > thereby most Pythonic) approach is over twice as fast as the fancy ones
> > (measurements on 2.2.2 seem to show similar results, all approaches
> > being about 20% slower than in 2.3, uniformly).
> > 
> > 
> > Alex
> 
> Wow, I feel vindicated now for never trying to wrap my brain around stuff
> like list comprehensions. *g*  

You are welcome to feel great about never having learned the meaning 
of the reduce builtin. But list comprehensions have nothing to do with 
it - they are just a shorthand notation for a loop that appends to a 
list. The performance of list comprehensions is exactly the same as the 
equivalent loop construct. In this case, though, the version of the
code using the list comprehension was not equivalent - it created a 
temporary list where none was necessary and was therefore slower than 
the trivial Pythonic implementation.

    Oren





More information about the Python-list mailing list