Pythonic way to sum n-th list element?
staschuk at telusplanet.net
Sat Apr 19 19:26:32 CEST 2003
Quoth Alex Martelli:
> "sum([y for y in x])" would be very simple and direct, IMHO; the
> "same" thing expressed with "reduce(operator.add,[y for y in x])"
> may only feel "simple and direct" to PhD's in maths -- it feels
> _abstruse_ to most people, in my opinion.
What if operator.add were changed to take any number of arguments?
Then you could write
operator.add(*[y for y in x])
Btw, I don't know about most people, but I don't have a PhD in
math, and reduce(operator.add, L) seems simple and clear to me.
(I do agree that there is a point beyond which abstraction brings
diminishing returns; but reduce is well inside that limit, imho.)
Steven Taschuk staschuk at telusplanet.net
"Our analysis begins with two outrageous benchmarks."
-- Clinger et al., "Implementation strategies for continuations"
More information about the Python-list