# The minimum of a Dict is not a Dict but a Pair

**URL:** <https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780>\
**Category:** New to Julia\
**Tags:** dictionary\
**Created:** [August 20, 2018, 6:49pm UTC](https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780 "2018-08-20T18:49:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![metanoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metanoid/32/3541_2.png) [@metanoid](https://discourse.julialang.org/u/metanoid)\
**Post date:** [August 20, 2018, 6:49pm UTC](https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780/1 "2018-08-20T18:49:21Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 20, 2018, 7:12pm UTC](https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780/2 "2018-08-20T19:12:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 21, 2018, 6:00am UTC](https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780/3 "2018-08-21T06:00:37Z")

</div>

> [@metanoid](#):
>
> (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)

Use `findmin`. It returns `(minvalue, index)`, or for a `Dict` it returns `(minvalue, key)`:

```julia
julia> d = Dict(:a=>5, :hello=>8, :z=>2)
Dict{Symbol,Int64} with 3 entries:
  :a => 5
  :hello => 8
  :z => 2

julia> findmin(d)
(2, :z)

julia> findmax(d)
(8, :hello)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 21, 2018, 6:25am UTC](https://discourse.julialang.org/t/the-minimum-of-a-dict-is-not-a-dict-but-a-pair/13780/4 "2018-08-21T06:25:02Z")

</div>

> [@rdeits](#):
>
> 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.

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 `Pair`s, and `Dict` as an indexable collection that accepts non-integer indexes.
