Pythonic way to sum n-th list element?

Steve Holden sholden at holdenweb.com
Mon Apr 21 09:36:05 EDT 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:KIioa.58844$T34.1577116 at news2.tin.it...
> Alex Martelli wrote:
>
> > Steven Taschuk wrote:
> >
> >> Quoth Alex Martelli:
> >>   [...]
[and ... again: sum() experiments ...]
>
> 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...
>

Only one way to resolve those doubts.

The use of sum() to concatenate a list (please, not a tuple!) of strings in
such a deterministic way is a masterstroke. It would surely deserve to
become immediately idiomatic.

looking-forward-to-the-python-dev-summari-ly y'rs -  steve
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list