On Apr 25, 7:42 pm, Paul Rubin <no.em...@nospam.invalid> wrote: > Raymond Hettinger <pyt...@rcn.com> writes: > > Here's a handy utility function for you guys to play with: > > http://code.activestate.com/recipes/577676/ > > 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 users. > 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(). Raymond -- http://mail.python.org/mailman/listinfo/python-list