I'd like to iterate over a subset of a dictionary to find the minimum value in the subset. I can use a generator expression to get the value:
>>> S = {'a', 'c'}
>>> D = {'a': 2, 'b': 0, 'c': 1, 'd': 3}
>>> min(D[k] for k in S)
1
To get the associated keys, I guess this is possible:
>>> subset_min = min(D[k] for k in S)
>>> [k for k, v in D.iteritems() if v == subset_min]
['c']
But surely there's a way that wouldn't require searching the dictionary again? I was hoping for something like min(D, key=D.get)
as discussed here, but I can't see how to apply it in this case.