Looping a Dictionary prints the 2nd element first

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> 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

Dict is not an ordered collection.

OrderedDict from OrderedCollections.jl will work though.

4 Likes