[Python-ideas] Fast sum() for non-numbers

Serhiy Storchaka storchaka at gmail.com
Wed Jul 3 12:10:59 CEST 2013


02.07.13 21:12, Sergey написав(ла):
> Alternatives to sum() for this use-case:
> * list comprehension is 270% slower than patched sum [2]
> * itertools.chain is 50%-100% slower than patched sum [3]

You forgot most straightforward alternative:

     result = []
     for x in iterable:
         result.extend(x)

I'm sure this is most popular solution for this problem.

> Questions that may arise if the patch is accepted:
> * sum() was rejecting strings because of this bug. If the bug gets fixed
>    should another patch allow sum() to accept strings?

No. Strings are immutable.

> * maybe in some distant future drop the second parameter (or make it
>    None by default) and allow calling sum for everything, making sum()
>    "the one obvious way" to sum up things?

No. sum() should work for empty sequences.




More information about the Python-ideas mailing list