Pythonic way to sum n-th list element?

Anna revanna at
Sat Apr 19 23:12:57 CEST 2003

On Sat, 19 Apr 2003 20:55:06 +0000, Alex Martelli wrote:

> So, I tried implementing sum in the simplest way, as a separate patch to
> operator.c (leaving add alone) -- THAT gives me 124 microseconds, a 50%
> speedup with respect to the simple for loop and clearly sufficient to
> justify sum's existence (together with its simplicity, clarity, and likely
> frequency of use).  As I was at it, I also ensured that operator.sum(x)
> would immediately delegate to ''.join(x) if x's first item is a string --
> this way it gets the same top performance as ''.join(x), avoiding the trap
> into which the "for xx in x: t+=xx" falls here (for x=map(str,range(999)),
> sum(x) clocks in at 79 microseconds, vs 78 for ''.join(x) and 1260 for the
> simple loop with += ...).
> Interesting... I have my doubts on whether Guido would accept such a patch
> just a week before a beta, but maybe I should try submitting it anyway...
> Alex



More information about the Python-list mailing list