Simple map/reduce utility function for data analysis

Raymond Hettinger python at
Tue Apr 26 14:12:33 EDT 2011

On Apr 25, 7:42 pm, Paul Rubin <no.em... at nospam.invalid> wrote:
> Raymond Hettinger <pyt... at> writes:
> > Here's a handy utility function for you guys to play with:
> >
> Cute, but why not use collections.defaultdict for the return dict?
> Untested:

My first draft had a defaultdict but that implementation detail would
get exposed to the user unless the return value was first coerced to a
regular dict.  Also, I avoided modern python features so the code
would run well on psyco and so that it would make sense to beginning

> Untested:
>   d = defaultdict(list)
>   for key,value in ifilter(bool,imap(mapper, data)):
>      d[key].append(value)
>   ...

Nice use of itertools.  FWIW, ifilter() will accept None for the first
argument -- that's a bit faster than using bool().


More information about the Python-list mailing list