472,129 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 software developers and data experts.

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 1221
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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.