473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

getting a key for a given value in the dict

440 256MB
Hi,

I would like to get a key for a given value in the dict.For example:

I/P:
Expand|Select|Wrap|Line Numbers
  1. Sample
  2. dict1 = { 100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
  3.  
Assume that the Keys and Values are unique in the above scenario.

I would like to search the respective key for the given value "100020".

O/P :
200

Is there any simple way,where I can search the 'key' for a given 'value',without using the "for" loop.

If any method available then it will save the time from searching in a bigger dictionary data


Thanks
PSB
Apr 13 '07 #1
3 2586
bvdet
2,851 Expert Mod 2GB
Hi,

I would like to get a key for a given value in the dict.For example:

I/P:
Expand|Select|Wrap|Line Numbers
  1. Sample
  2. dict1 = { 100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
  3.  
Assume that the Keys and Values are unique in the above scenario.

I would like to search the respective key for the given value "100020".

O/P :
200

Is there any simple way,where I can search the 'key' for a given 'value',without using the "for" loop.

If any method available then it will save the time from searching in a bigger dictionary data


Thanks
PSB
The for loop is the simplest way I know of.
Expand|Select|Wrap|Line Numbers
  1. # Search in dict.values() for specific value, return key
  2.  
  3. dd = {100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
  4. target = 100020
  5.  
  6. for key in dd:
  7.     if target in dd[key]:
  8.         break
  9.  
  10. print key
If there is a more efficient way, I would like to hear about it also!
Apr 13 '07 #2
bartonc
6,596 Expert 4TB
Search TheScripts for "inverse dictionary" and you'll see that this is a very common practice.
Apr 13 '07 #3
ghostdog74
511 Expert 256MB
this is the usual way. however you could always create a function once, then use it whenever you need to find something in a dictionary. Just pass in the dictionary and the search target to that function or something like that.
Apr 14 '07 #4

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

Similar topics

15
by: Andy C | last post by:
I am new to python, so please bear with me if I am making some conceptual error. Basically I want to create a graph with an adjacency list representation, but I don't want any of the adjacency...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
1
by: Yereth | last post by:
Hi again, still working on my xslt program and I ran into another problem. There is a file which a contains the following structure: <key>1</key> <dict>         ... </dict> <key>2</key>
31
by: Thanos Tsouanas | last post by:
Hello. I would like to have a quick way to create dicts from object, so that a call to foo would return obj.bar. The following works, but I would prefer to use a built-in way if one exists. ...
2
by: tonci.tomic | last post by:
I have IDictionary with integers as keys and values. Suppose I want to set value for key=42 to 1 if entry doesn't exist in container. Otherwise, I want to increment value. I ended with: ...
6
by: Andrew Robinson | last post by:
given a Hashtable and an Index, how do I retrieve the key? Hashtable h = new Hashtable(); h.Add("red", 3); h.Add("blue", 99); h.Add("green", 33); how do I get the key at index 2? h.Keys...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
20
by: Pat | last post by:
I know it's not "fair" to compare language features, but it seems to me (a Python newbie) that appending a new key/value to a dict in Python is awfully cumbersome. In Python, this is the best...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.