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

Stefan Behnel stefan_ml at behnel.de
Fri Jul 5 12:50:54 CEST 2013


Sergey, 02.07.2013 20:12:
> sum() is a great function. It is the "obvious way" to add things.
> Unfortunately sometimes it's slower than it could be.
> 
> The problem is that code:
>   sum([[1,2,3]]*1000000, [])
> takes forever to complete. Let's fix that!

No, please. Using sum() on lists is not more than a hack that seems to be a
cool idea but isn't. Seriously - what's the sum of lists? Intuitively, it
makes no sense at all to say sum(lists). What you want is the
concatenation, not the sum. Please don't stuff something as simple as sum()
with a nonsensical misuse case.

Stefan




More information about the Python-ideas mailing list