By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,806 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,806 IT Pros & Developers. It's quick & easy.

accessing dictonary keys thru values

rhitam30111985
100+
P: 112
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
Share this Question
Share on Google+
2 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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
100+
P: 112
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.