I’m using the minimum function to find the entry in a Dict which has the smallest value. Then I intended to use the keys function on the result to get the key which corresponds to the smallest value in the Dict.
However, the keys function does not work, as the result of calling minimum on a Dict is not a smaller Dict but a Pair. This is counter-intuitive to me.
Can anybody explain why the minimum of a Dict is not a one-element Dict, but a Pair?
It’s not a problem per se, I just want to understand.
(P.S. If there’s a better way to do what I want - i.e. find the key that corresponds to the minimum value in a Dict - please let me know)
The current behavior seems pretty self-consistent to me. The minimum of a Vector{X} is an element of type X not a smaller vector. Likewise, a Dict can be thought of as a collection of key-value pairs, so the minimum of a Dict should be a single key-value pair, not a smaller Dict.
However, note that minimum(::Dict) probably isn’t giving you the answer you expect, as it will give you the minimum of the key-value pairs, whereas you want the key corresponding to the minimum value.
I don’t think there’s a built-in function for what you want, but it should be straightforward and efficient to write one.
I’m not completely sure if it seems consistent to me. minimum(::Dict) returns the key-val pair that corresponds to the smallest key, while findmin returns (val, key) that corresponds to the smallest value.
The corresponding behaviour for an indexable collection would be: minimum returns the index-value pair corresponding to the smallest index, and findmin returns (val, index) corresponding to the smallest value.
In that sense, findmin seems to behave consistently between dicts and arrays, while minimum is slightly mind-bending. There is some tension between the model of Dict as a vector of Pairs, and Dict as an indexable collection that accepts non-integer indexes.