newbie: dictionary - howto get key value

Richard Brodie R.Brodie at rl.ac.uk
Thu Mar 10 13:05:15 EST 2005


<Michael.Coll-Barth at VerizonWireless.com> wrote in message
news:mailman.209.1110476089.1799.python-list at python.org...

> test = 3   #find person with this number
> for x in xrange(len(phone.keys())):
>   print x
>    if phone[phone.keys()[x]] == test:
>       print phone.keys()[x]
>       break
>
>Being a newbie myself, I'd love a little critique on the above.  Be kind

Constructs like xrange(len(something)) are fairly typical for somone
moving to Python from another language; usually there is a more
idiomatic alternative.

In this case, writing "for k in phone.keys()" would be a good start.










More information about the Python-list mailing list