[Tutor] Finding the key for a value in a dictionary.

Wesley Brooks wesbrooks at gmail.com
Thu Jan 11 11:21:06 CET 2007


Dear Users,

I'm trying to find the key of a unique value within a dictionary. Is
the code bellow a safe way of doing so, or is there a better way of
doing it?

a = {'de':'df', 'gf':'hg'}
key = a.keys()[a.values().index('hg')]

Thanks for your help,

Yours Faithfully,

Wesley Brooks


More information about the Tutor mailing list