# Enumerate with keys from a dict

**URL:** https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413
**Category:** New to Julia
**Created:** [July 2, 2020, 7:33am UTC](https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413 "2020-07-02T07:33:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)
#### Post date: [July 2, 2020, 7:33am UTC](https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413/1 "2020-07-02T07:33:21Z")

</div>

I am wondering if the behaviour in the picture below is intended? Shouldn’t `i` stop at `9999`?

![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cfdaa97209b898ee952f3b9f6bc94bbfeb553105.png)

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 2, 2020, 7:44am UTC](https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413/2 "2020-07-02T07:44:49Z")

</div>

I cannot reproduce:

```julia
julia> d = Dict((i=>i for i=1:9999));

julia> for (i,v) in enumerate(keys(d))
       if i==10000
       @show i
       end
       end

```

Can you provide a MWE? (And please use text and not pics as those cannot be copy pasted)

---

<div class="post-metadata">

### Author: ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)
#### Post date: [July 2, 2020, 7:57am UTC](https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413/3 "2020-07-02T07:57:40Z")

</div>

Thanks for the fast reply! I guess it might be because I am using `Threads.@threads` beforehand.

```julia
succeded = Dict{Int,Any}()
Threads.@threads for i = 1:1000
   succeded[i] = ["a"]
end
length(keys(succeded)) # returns 806

```

Does that make sense?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 2, 2020, 8:04am UTC](https://discourse.julialang.org/t/enumerate-with-keys-from-a-dict/42413/4 "2020-07-02T08:04:36Z")

</div>

The problem here is that `Dict` is not thread safe and you are therefore creating race conditions. You might be interested in this thread:

> [@Can dicts be threadsafe?](https://discourse.julialang.org/t/can-dicts-be-threadsafe/27172):
>
> dict = Dict{Int64,Vector{Float64}}() nentries = 5e5 Threads.@threads for n in 1:nentries dict[n] = [rand() for i in 1:100] end @assert length(dict) == nentries The assertion fails for me in Julia 1.1.0, as the dict is shorter than the number of entries. Is there a way to safely use dicts with threads?
