'inverting' a dict

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


Yermat wrote:

> or even shorter
> 
>>>>d = {'key1' : ('value1','value2'), 'key2': ('value3',) }
> 
> 
>>>>dict([(v,k) for k in d.iterkeys() for v in d[k]])
> 
> {'value3': 'key2', 'value2': 'key1', 'value1': 'key1'}

Yep, thanks, that must have been the solution that I was looking for!
I understand list comprehensions, but keep forgetting about the
multiple-loop-kind.

--Irmen.





More information about the Python-list mailing list