Pythonic way to sum n-th list element?

Steven Taschuk staschuk at telusplanet.net
Sun Apr 20 02:31:53 EDT 2003


Quoth Alex Martelli:
> Steven Taschuk wrote:
  [...]
> > What if operator.add were changed to take any number of arguments?
> > Then you could write
> >     operator.add(*[y[1] for y in x])
> > Clear?  Abstruse?
> 
> That would be quite acceptable.  [...]
> What would you have operator.add return when called with no
> arguments, though?  Either 0 or 0.0 or '' or ... have problems.

That is a problem.  Of course, reduce solves it tidily.  :)

  [...]
> Guess we'll have to agree to disagree.  [...]  I now
> think that reduce is more trouble than it's worth and Python
> would be better off without it, if it was being designed from
> scratch today -- it would not substantially reduce (:-) Python's 
> power and WOULD substantially ease the teaching/&c task.  [...]

I'll defer to your experience teaching Python.  (I have none.)

On reflection, I realize that I have prior experience (from math
reading and play) with many concrete examples of what reduce
abstracts: big sigma notation for sums, big pi notation for
products, similar notations for set unions and intersections,
etc..  Familiarity with these concrete examples makes the
abstraction of reduce sit very comfortably.  "I see, reduce turns
+ into big sigma."  But perhaps such familiarity is not typical in
Python's, er, target market.

-- 
Steven Taschuk                             staschuk at telusplanet.net
"I may be wrong but I'm positive."  -- _Friday_, Robert A. Heinlein





More information about the Python-list mailing list