In article <[EMAIL PROTECTED]>, Skip Montanaro <[EMAIL PROTECTED]> wrote: > >Assuming your dictionary defines a one-to-one mapping, just invert it: > > >>> forward = {10 : 50, 2 : 12, 4 : 43} > >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()]) > >>> print forward > {10: 50, 4: 43, 2: 12} > >>> print reverse > {50: 10, 43: 4, 12: 2} > >That doubles your storage, so you'll have to trade that off against the >speed gain of not having to loop over the entire dictionary.
To be precise, it doubles the storage of the *dictionary*, but it does *NOT* double the storage of the keys and items. Depending on how big those are, the cost of building a second dict might be mostly lost in the noise. -- Aahz ([EMAIL PROTECTED]) <*> http://www.pythoncraft.com/ "19. A language that doesn't affect the way you think about programming, is not worth knowing." --Alan Perlis -- http://mail.python.org/mailman/listinfo/python-list