Idea for reduce (Re: Question regarding a recent article on informit.com)

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Fri Mar 21 02:32:21 CET 2003


Ben S wrote:
> Are there any good tutorials of examples of list comprehension use? In
> particular, I'm interested in how to replicate the functionality of
> reduce? Map and filter seem easy to do, though.

As has been pointed out, list comprehensions don't (and
aren't meant to) substitute for reduce, only for map
and filter.

Something else again would be needed if we wanted a
reduce-substitute. While I'm far from certain that it
would be worth it, it's interesting to speculate on what
a Pythonic reduce-syntax might look like.

We want something that says things like "add up this
list of numbers". Let's see... if we were to make
"up" a new (possibly pseudo) keyword...

   from operator import add
   numbers = [1, 17, 42]
   total = add up numbers

Howzat look? :-]

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list