# Julian way of comparing two dictionaries

**URL:** <https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693>\
**Category:** General Usage\
**Tags:** question, dictionary, julian-way\
**Created:** [September 3, 2017, 3:52pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693 "2017-09-03T15:52:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 3, 2017, 3:52pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/1 "2017-09-03T15:52:10Z")

</div>

How do you compare two dictionaries?

```julia
d1 = Dict(1=>2., 3=>4.)
d2 = Dict(3=>4., 1=>2.)
d3 = Dict(1=>0., 3=>4.)

d1 == d2 # true
d1 == d3 # false

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [September 3, 2017, 4:00pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/2 "2017-09-03T16:00:34Z")

</div>

I don’t understand the question. If you type `@which d1 == d3`, it points to the [source code](https://github.com/JuliaLang/julia/blob/903644385b91ed8d95e5e3a5716c089dd1f1b08a/base/associative.jl#L338) that shows how Julia does it. Is that what you want to know?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 3, 2017, 4:12pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/3 "2017-09-03T16:12:08Z")

</div>

@cstjean, I just realized that my problem is something else. I have a test that is evaluating as false, but the dictionaries are the same:

```julia
Test Failed
  Expression: GeoStats.mapping(mapper1D, :value) == Dict(23 => 0.0347232, 45 => 0.282193, 57 => 0.729891, 68 => 0.341069, 34 => 0.192102, 90 => 0.549381, 79 => 0.577662, 12 => 0.980171, 1 => 0.191499)
   Evaluated: Dict(23=>0.0347232,45=>0.282193,57=>0.729891,68=>0.341069,34=>0.192102,90=>0.549381,79=>0.577662,12=>0.980171,1=>0.191499) == Dict(23=>0.0347232,68=>0.341069,90=>0.549381,12=>0.980171,34=>0.192102,1=>0.191499,79=>0.577662,57=>0.729891,45=>0.282193)

```

Maybe it is the type of the dictionary entries that is not matching? I will double check it here.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 3, 2017, 4:16pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/4 "2017-09-03T16:16:37Z")

</div>

If I copy/paste the evaluation that is failing in the tests in the Julia prompt, it doesn’t fail:

```julia
Dict(23=>0.0347232,45=>0.282193,57=>0.729891,68=>0.341069,34=>0.192102,90=>0.549381,79=>0.577662,12=>0.980171,1=>0.191499) == Dict(23=>0.0347232,68=>0.341069,90=>0.549381,12=>0.980171,34=>0.192102,1=>0.191499,79=>0.577662,57=>0.729891,45=>0.282193)

```

What is the cause of the issue?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 3, 2017, 4:33pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/5 "2017-09-03T16:33:08Z")

</div>

```nohighlight
julia> d = Dict(23=>0.0347232,45=>0.282193,57=>0.729891,68=>0.341069,34=>0.192102,90=>0.549381,79=>0.577662,12=>0.980171,1=>0.191499);

julia> d == deepcopy(d)
true

```

works for me

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 3, 2017, 4:33pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/6 "2017-09-03T16:33:50Z")

</div>

@jw3126, the line I copy/pasted above is working for me as well, do you think it is a rounding issue?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 3, 2017, 4:34pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/7 "2017-09-03T16:34:14Z")

</div>

Probably its a rounding issue. Only the first few digits of d are displayed. In your tests you have to dicts whose values are the same only up to the first few digits.

```nohighlight
julia> d1 = Dict(1 => 9.87654321)
Dict{Int64,Float64} with 1 entry:
  1 => 9.87654

julia> d2 = Dict(1 => 9.87654322)
Dict{Int64,Float64} with 1 entry:
  1 => 9.87654

julia> using Base.Test

julia> @test d1 == d2
Test Failed
  Expression: d1 == d2
   Evaluated: Dict(1=>9.87654) == Dict(1=>9.87654)

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [September 7, 2017, 12:30pm UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/8 "2017-09-07T12:30:27Z")

</div>

`Base` does not define `isapprox` for `Dicts`. Something like this might be useful.

```julia
function Base.isapprox(l::Associative, r::Associative)
    l === r && return true
    if isa(l,ObjectIdDict) != isa(r,ObjectIdDict)
        return false
    end
    if length(l) != length(r) return false end
    for pair in l
        if !in(pair, r, isapprox)
            return false
        end
    end
    true
end

```

But, allowing tolerances, etc. would be necessary.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [July 31, 2018, 8:09am UTC](https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/9 "2018-07-31T08:09:31Z")

</div>

And what do you think about function `isequal(d1, d2)`? Mainly if the `Dict` contains `NaN`.
