# Looping a Dictionary prints the 2nd element first

**URL:** <https://discourse.julialang.org/t/looping-a-dictionary-prints-the-2nd-element-first/78275>\
**Category:** New to Julia\
**Tags:** question, dictionary\
**Created:** [March 22, 2022, 1:32pm UTC](https://discourse.julialang.org/t/looping-a-dictionary-prints-the-2nd-element-first/78275 "2022-03-22T13:32:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LispyArnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lispyarnuld/32/34670_2.png) [@LispyArnuld](https://discourse.julialang.org/u/LispyArnuld)\
**Post date:** [March 22, 2022, 1:32pm UTC](https://discourse.julialang.org/t/looping-a-dictionary-prints-the-2nd-element-first/78275/1 "2022-03-22T13:32:31Z")

</div>

If I create a dictionary and then loop over it using a for loop, I expect the printing to go in order from first element to last. Here it’s different. Second element is being printed first by for loop. I wonder why.

```julia
julia> d = Dict([("A", 1), ("B", 2), ("C", 3)])
Dict{String, Int64} with 3 entries:
  "B" => 2
  "A" => 1
  "C" => 3

julia> for e in d
           println(e)
       end
"B" => 2
"A" => 1
"C" => 3

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 22, 2022, 1:35pm UTC](https://discourse.julialang.org/t/looping-a-dictionary-prints-the-2nd-element-first/78275/2 "2022-03-22T13:35:46Z")

</div>

`Dict` is not an ordered collection.

`OrderedDict` from OrderedCollections.jl will work though.

> **[GitHub - JuliaCollections/OrderedCollections.jl: Julia implementation of...](https://github.com/JuliaCollections/OrderedCollections.jl)**
>
> Julia implementation of associative containers that preserve insertion order - GitHub - JuliaCollections/OrderedCollections.jl: Julia implementation of associative containers that preserve inserti...
