Python one-liner?

Tim Chase python.list at tim.thechases.com
Sat Apr 14 07:26:33 EDT 2012


On 04/13/12 22:54, Chris Angelico wrote:
> Yes, that would be the right method to use. I'd not bother with the
> function and map() though, and simply iterate:
>
> d = {}
> for val in l:
>   d.setdefault(f(val), []).append(val)

Or make d a defaultdict:

   from collections import defaultdict
   d = defaultdict(list)
   for val in l:
     d[f(val)].append(val)

-tkc





More information about the Python-list mailing list