Is there any straightforward way of finding a key by knowing the value within a dictionary?
All I can think of is this:
key = [key for key, value in dict_obj.items() if value == 'value'][0]
Is there any straightforward way of finding a key by knowing the value within a dictionary?
All I can think of is this:
key = [key for key, value in dict_obj.items() if value == 'value'][0]
There are cases where a dictionary is a one:one mapping
Eg,
Your approach is ok if you are only doing a single lookup. However if you need to do more than one lookup it will be more efficient to create an inverse dictionary
If there is a possibility of multiple keys with the same value, you will need to specify the desired behaviour in this case.
If your Python is 2.6 or older, you can use
There is none. Don't forget that the value may be found on any number of keys, including 0 or more than 1.
There isn't one as far as I know of, one way however to do it is to create a dict for normal lookup by key and another dict for reverse lookup by value.
There's an example of such an implementation here:
http://code.activestate.com/recipes/415903-two-dict-classes-which-can-lookup-keys-by-value-an/
This does mean that looking up the keys for a value could result in multiple results which can be returned as a simple list.
I know this might be considered 'wasteful', but in this scenario I often store the key as an additional column in the value record:
it's a tradeoff and feels wrong, but it's simple and works and of course depends on values being tuples rather than simple values.
As value could be non-existent in dict, a more pythonic and auto-documented code would be:
Indeed dicts aren't made to answer such problematics, if you encounter this problem on a new designed program then you should probably review your design.
This version is 26% shorter than yours but functions identically, even for redundant/ambiguous values (returns the first match, as yours does). However, it is probably twice as slow as yours, because it creates a list from the dict twice.
Or if you prefer brevity over readability you can save one more character with
And if you prefer efficiency, @PaulMcGuire's approach is better. If there are lots of keys that share the same value it's more efficient not to instantiate that list of keys with a list comprehension and instead use use a generator: