473,581 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing dictonary keys thru values

rhitam30111985
112 New Member
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 1276
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
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

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

Similar topics

17
2419
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next = gen.next() finally:
3
1242
by: Gonzalo Sainz-Trápaga (GomoX) | last post by:
Hi, I've posted this to mod_python@modpython.org and got no answers, so i'm reposting it here, I really hope someone can help me out. ------ Hi, I'm building my first application with mod_python. I am using Ian Bicking's SQLObject, validators and HTMLfill among other tools. I built a custom handler that parses the url and loads an...
6
13250
by: wASP | last post by:
Hi again, I'm having a problem accessing the ViewState object. I'm using the following two functions - as copied from the MS docs - and state.count is zero in the first - and the while loop in the second function never iterates: /* ------------------------------------------------------------------ */ /*...
5
2471
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything it would be more efficient to access directly. I also don't think that you would need to do any special validation since the class developer knows...
14
3447
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the...
8
2072
by: Martin Pöpping | last post by:
Hello, I´ve implemented a Hashtable with Int-Keys and Double Values. Now I want to get the i-th Int-Key of my hashtable. How do I do that? I tried it like that: ICollection intKeys = myHashtable.Keys;
3
6603
by: =?Utf-8?B?TmF2bml0?= | last post by:
Hi, I have the following scenario:- 1) I have a web part 2) This web part uses a class library 3) In the Class Library I want to save and retrieve values from the ViewState I am not able to do this since ViewState is always associated to a control. What are the possible options with which I can achieve this?. I don’t want to pass the...
13
1789
by: Iris83 | last post by:
Hi, I have a question about converting some of the data in my dataset but leave some data the way it is. I have a hash and if the key is present and the Value of the Hash equals B it should convert these number but if B is not present the data should remain the same. my output is that all the data is converted not in the only the cases...
0
787
by: gurmeet07 | last post by:
hi guys if i m navigating among the records saved in hash table...........i.e. first record , previous record , next record and last record as navigating buttons can be there on a form.................. if i want to access the first key , previous key , next key and last key from the hash table how to access that ?????? and then accordingly...
2
5802
by: Sjoerd | last post by:
On Mon, 08 Sep 2008 00:11:38 -0700, mouac01@yahoo.com wrote: Loop through the array and make an array of all keys. Then loop through the array again and add the missing keys.
0
7860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8139
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8166
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.