My question is: How can I get a dictionary key using a dictionary value?
d={'dict2': {1: 'one', 2: 'two'}, 'dict1': {3: 'three', 4: 'four'}}
I want to get dict2
the key of the key of two
.
Thanks.
My question is: How can I get a dictionary key using a dictionary value?
d={'dict2': {1: 'one', 2: 'two'}, 'dict1': {3: 'three', 4: 'four'}}
I want to get dict2
the key of the key of two
.
Thanks.
To handle the nested dictionaries I would do just as senderle's answer states.
However if in the future it does not contain nested dictionaries, be very careful doing a simple reversal. By design the dictionary keys are unique, but the values do not have this requirement.
If you have values that are the same for multiple keys, when reversing the dictionary you will lose all but one of them. And because dictionaries are not sorted, you could lose different data arbitrarily.
Example of reversal working:
Example of reversal failure: Note that
dict4
is lostIf you don't want to reverse the dictionary, here's another possible solution:
Write code to reverse the dictionary (i.e. create a new dictionary that maps the values of the old one to the keys of the old one).
Since you seem to be dealing with nested dictionaries, this will obviously be trickier. Figure out the least you need to get your problem solved and code that up (i.e. don't create a solution that will work arbitrary depths of nesting if your problem only deals with dicts in dicts which in turn don't have any dicts)
I don't know about the best solution, but one possibility is reversing the dictionary (so that values becomes keys) and then just doing a normal key lookup. This will reverse a dictionary:
So given "val1", I can just do:
to find the corresponding key. There are obvious problems with this solution -- for example, if your values aren't unique, you're going to lose some information.
Here's a recursive solution that can handle arbitrarily nested dictionaries:
It's not as efficient in the long run as a "reverse dictionary," but if you aren't doing such reverse searches frequently, it probably doesn't matter. (Note that you have to explicitly compare the result of
dict_find_recursive(d[k], target)
toFalse
because otherwise falsy keys like''
cause the search to fail. In fact, even this version fails ifFalse
is used as a key; a fully general solution would use a unique sentinelobject()
to indicate falseness.)A few usage examples:
If you want to reverse an arbitrarily nested set of dictionaries, recursive generators are your friend:
The above simply lists all the values in the dictionary that aren't mappings. You can then map each of those values to a key like so:
This generates a iterable of tuples, so no information is lost:
If you know that all your non-mapping values are hashable -- and if you know they are unique, or if you don't care about collisions -- then just pass the resulting tuples to
dict()
:here's an example nobody thinks about: (could be used similarly)
result:
maybe not the best of methods, but it works for what I need it for.