sum for sequences?

Patrick Maupin pmaupin at gmail.com
Sun Mar 28 20:16:25 CEST 2010


On Mar 28, 12:34 pm, Steve Howell <showel... at yahoo.com> wrote:
> FYI I later obtained similar results with the more general:
>                   accum += sublist

Yeah, but you still have to create an object of the correct type for
accum.  And for summing small lists, that will actually increase the
runtime.  (Worst case, a list of a single item: you have to create the
accum object based on the type of the start value, then do two += into
it, for the start value and the first item in the list, vs just doing
a single + which automatically creates an object.)

Personally, I think the most general approach, if sum were open to
enhancements, would be to automatically apply Alf's suggestion of "+"
on summing the first item to the start value, and "+=" on subsequent
items.  Also, you *could* add a parameter to sum(), for example,
default "partialsums=False" that would allow the use of partial sums
in those cases where that might give better behavior (such as floats
and tuples).




More information about the Python-list mailing list