newbie: dictionary - howto get key value

Tim Roberts timr at probo.com
Sat Mar 12 01:48:31 EST 2005


"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() ) )
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list