469,602 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

accessing dictonary keys thru values

rhitam30111985
112 100+
hi every1
i am an newbie python programmer.. i need to know how do i access a key when given the value in an associative array ? ie.. eg .. in a telephone directory stored as a dictionary..
if a enter a telephone number i should get the name corresponding to it.
Aug 21 '07 #1
2 1170
bvdet
2,851 Expert Mod 2GB
hi every1
i am an newbie python programmer.. i need to know how do i access a key when given the value in an associative array ? ie.. eg .. in a telephone directory stored as a dictionary..
if a enter a telephone number i should get the name corresponding to it.
Try this:
Expand|Select|Wrap|Line Numbers
  1. value = '(800) 222-3333'
  2. key = dd.keys()[dd.values().index(value)]
Lists are ordered objects. I believe the list object dd.values() will be ordered the same as dd.keys().
Aug 21 '07 #2
rhitam30111985
112 100+
Try this:
Expand|Select|Wrap|Line Numbers
  1. value = '(800) 222-3333'
  2. key = dd.keys()[dd.values().index(value)]
Lists are ordered objects. I believe the list object dd.values() will be ordered the same as dd.keys().
hi bvdet,
thanks a lot.. code works..
Aug 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by Andrae Muys | last post: by
3 posts views Thread by Gonzalo Sainz-Trápaga (GomoX) | last post: by
6 posts views Thread by wASP | last post: by
8 posts views Thread by Martin Pöpping | last post: by
3 posts views Thread by =?Utf-8?B?TmF2bml0?= | last post: by
13 posts views Thread by Iris83 | last post: by
2 posts views Thread by Sjoerd | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.