Reverse dictionnary

Alex Martelli aleax at aleax.it
Fri Nov 15 07:49:34 EST 2002


Uwe Schmitt wrote:
   ...
>     d_reverse=dict(zip(d.values(), d.keys())
> 
> works, but I'm not sure if d.values() and d.keys() are allways
> in right order...

Yes, that's guaranteed by the language reference: methods keys,
values, items, iterkeys, itervalues, and iteritems, called on
the same dict object WITHOUT any intervening change, always
return items (or halves thereof -- key and value being two
halves of each item) in the SAME (arbitrary) order.


Alex




More information about the Python-list mailing list