'inverting' a dict

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Wed Dec 31 14:12:51 CET 2003

Jeff Epler wrote:
> Does this listcomp give the right result?
>     dict([(v, k) for k, vs in original.iteritems() for v in vs])

Yes it does, and it's even more concise than Yermat's solution

     dict([(v,k) for k in d.iterkeys() for v in d[k]])

because it avoids a dict lookup every loop cycle. Thanks Jeff!


More information about the Python-list mailing list