Hi,
I would like to get a key for a given value in the dict.For example:
I/P: - Sample
-
dict1 = { 100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
-
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
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: - Sample
-
dict1 = { 100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
-
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. - # Search in dict.values() for specific value, return key
-
-
dd = {100:[10001,10002,10006,10009,10008],200:[100010,100020,100023],300:[100030,100031,1000041]}
-
target = 100020
-
-
for key in dd:
-
if target in dd[key]:
-
break
-
-
print key
If there is a more efficient way, I would like to hear about it also!
Search TheScripts for "inverse dictionary" and you'll see that this is a very common practice.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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>
|
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. ...
|
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:
...
|
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...
|
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...
|
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:...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |