# Dict can't find a key it already said is there?

**URL:** <https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 2, 2020, 5:19pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088 "2020-02-02T17:19:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmz/32/9861_2.png) [@rmz](https://discourse.julialang.org/u/rmz)\
**Post date:** [February 2, 2020, 5:19pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/1 "2020-02-02T17:19:06Z")

</div>

I’m confused I have this piece of code (see below, the important part is the the push! … line), it will push a value to a set located in a dict at a key (called “coord”) that we have already verified exists in the dict using a “`coord in dict.keys`” test. Mostly this code runs well, but from time to time, it fails with an error message like ` **ERROR:** KeyError: key (487, 466) not found"` (the actual numbers vary between the tests).

I’m very confused by this. Also I’m not very experienced with Julia, so there may easily be stupid things I’ve done :-).

The full source code can be found here: [https://github.com/la3lma/julia-playground/blob/master/ParticleMovement.jl](https://github.com/la3lma/julia-playground/blob/master/ParticleMovement.jl)

To reproduce the error, run “runSmoketest(10)” after loading the module.

```julia
  function partition(particles::Set{ParticleState}, xdim::Int64, ydim::Int64, maxx::Float64, maxy::Float64)
      dict = Dict{Tuple{Int64, Int64}, Set{ParticleState}}()
      for p in particles
          coord = pointToImageCoord(p.pos, xdim, ydim, maxx, maxy)
      	  if (coord in dict.keys)
            push!(dict[coord], p)
          else
            newSet = Set{ParticleState}([p])
            dict[coord] = newSet
          end
      end
      return dict
   end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 2, 2020, 5:54pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/2 "2020-02-02T17:54:14Z")

</div>

> [@rmz](#):
>
> `coord in dict.keys` ”

That is not how you do it, you do `haskey(dict, coord)`. Remember that Julia doesn’t have classes so you basically never call methods like that.

---

<div class="post-metadata">

**Author:** ![rmz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmz/32/9861_2.png) [@rmz](https://discourse.julialang.org/u/rmz)\
**Post date:** [February 2, 2020, 7:01pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/3 "2020-02-02T19:01:34Z")

</div>

Thank you so much! That solved the problem (and tought me a lesson .-) )

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [February 2, 2020, 8:18pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/4 "2020-02-02T20:18:19Z")

</div>

> [@rmz](#):
>
> if (coord in dict.keys) push!(dict[coord], p)

The right thing here would be `keys(dict)`. Your `dict.keys` creates a strange behaviour as in the following example:

```julia
myD = Dict{Tuple{Int, Int}, Int}()

for i in 1:100
    myD[(rand(1:100), rand(1:100))] = rand(1:100)
end

julia> collect(pairs(myD))[1:5]
5-element Array{Pair{Tuple{Int64,Int64},Int64},1}:
 (76, 69) => 3 
 (19, 13) => 13
 (15, 23) => 93
 (68, 81) => 57
  (85, 3) => 47

```

everything fine so far, now with `keys(myD)`:

```julia
for i in keys(myD)
    myD[i] = rand(1:100)
end

julia> collect(pairs(myD))[1:5]
5-element Array{Pair{Tuple{Int64,Int64},Int64},1}:
 (76, 69) => 2 
 (19, 13) => 1 
 (15, 23) => 34
 (68, 81) => 98
  (85, 3) => 51

```

still fine, now with your `dict.keys`:

```julia
for i in myD.keys
    myD[i] = rand(1:100)
end

julia> collect(pairs(myD))[1:5]
5-element Array{Pair{Tuple{Int64,Int64},Int64},1}:
                                   (15, 39) => 95 
 (7956008003829065844, 7022836293254017892) => 41 
 (3755189675490410920, 2097865092682005547) => 26 
  (128118608645467904, 5841654826181606699) => 85 
                      (-8, 140311374223048) => 100

julia> length(myD)
2142

```

Buuum, strange that that even worked so far … The reason seems to be that `myD.keys` gives something entirely different from `keys(myD)`.

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [February 2, 2020, 8:41pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/5 "2020-02-02T20:41:04Z")

</div>

Shouldn’t “keys” be renamed to something like “key\_slots” and then with Base.getproperty we can redefine “keys” to return only valid keys? I really like that all internals are exposed but this behavior seems to be very error prone.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 2, 2020, 10:53pm UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/6 "2020-02-02T22:53:57Z")

</div>

You can actually remove `in` check and simplify whole construction to

```julia
push!(get!(dict, coord, Set{ParticleState}()), p)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 3, 2020, 7:33am UTC](https://discourse.julialang.org/t/dict-cant-find-a-key-it-already-said-is-there/34088/7 "2020-02-03T07:33:34Z")

</div>

> [@Jakub\_Wronowski](#):
>
> I really like that all internals are exposed but this behavior seems to be very error prone.

Don’t use internals, use the API.
