newbie: dictionary - howto get key value
Joal Heagney
joal at bigpond.net.au
Mon Mar 14 00:02:25 EST 2005
Tim Roberts wrote:
> "G. Völkl" <gm.voelkl at t-online.de> wrote:
>
>>I use a dictionary:
>>
>>phone = {'mike':10,'sue':8,'john':3}
>>
>>phone['mike'] --> 10
>>
>>I want to know who has number 3?
>>
>>3 --> 'john'
>>
>>How to get it in the python way ?
>
>
> If you need to do this a lot, just keep two dictionaries, where the keys in
> each are the values in the other.
>
> reversephone = dict( zip( phone.values(), phone.keys() ) )
(Been away from python for a while, so forgive me if I'm asking a silly
question.)
Does python guarantee that the lists given by phone.values() and
phone.keys() are in mutual order? Or is it possible that python will
return the lists in different orders for .values() and .keys()?
Joal
More information about the Python-list
mailing list