Python one-liner?

Chris Angelico rosuav at gmail.com
Fri Apr 13 23:54:28 EDT 2012


On Sat, Apr 14, 2012 at 1:44 PM, Evan Driscoll <edriscoll at wisc.edu> wrote:
> Ha ha, sorry I can't read right now apparently. dict.setdefault does
> exactly what I wanted.
>
> (The name just prompted another interpretation in my mind which doesn't
> work and I got tunnel vision. I'll stop spamming now, and we return to
> your regularly scheduled broadcast.)

Yeah, it's an oddly named method. But I can't think of any name for it
that isn't odd.

I was just writing up a reply when your third message came in, but
there's one small aspect that's still worth mentioning, so I'll quote
it.

----

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)

Enjoy!

ChrisA



More information about the Python-list mailing list