How to get a key from dictionary?
Gustavo Cordova
gcordova at hebmex.com
Mon Mar 25 17:25:44 EST 2002
>
> Hi,
> Is there a possibility to get, from a dictionary, a key
> according to a value ?
> For example I have a dictionary
>
> dict={'aa':1,'bb':2}
>
> and
> dict['aa']
> is 1
>
> But how can I for value 1 find out key? (That is here 'aa')
>
> Thank you for help
> Ladislav
>
You're welcome, here's an example:
>>> Dict = { "gustavo":32, "pedro":31, "campa":25 }
>>> K = Dict.keys()
>>> V = Dict.values()
>>> K
['campa', 'pedro', 'gustavo']
>>> V
[25, 31, 32]
>>> V.index(31)
1
>>> K[V.index(31)]
'pedro'
>>> Dict.keys()[Dict.values().index(32)]
'gustavo'
>>>
More information about the Python-list
mailing list