Possible Duplicate:
Python dictionaries – find second character in a 2-character string which yields minimum value
I would like to submit the first item of a tuple key and return the remaining item of that key which minimizes the tuple key value.
For example:
d = {('a','b'): 100,
('a','c'): 200,
('a','d'): 500}
If I were to pass in 'a', I would like to return 'b'.
Sorting is unnecessary here:
This works by using a generator expression to give only the keys in the dictionary whose first element matches the input, and uses as the associated “weight” (unfortunately named the “key” here in this context) the associated value in the dictionary. Dictionaries have a
.getmethod which returns the value given a specific key, so that’s the natural one to use.Note that in the case of ties, this returns an arbitrary key.