A Dict preserving the insertion order?

Is there a dictionary which preserves the adding order.

julia> a = Dict{Symbol,Int}()
Dict{Symbol,Int64} with 0 entries

julia> a[:a] =3
3

julia> a
Dict{Symbol,Int64} with 1 entry:
  :a => 3

julia> a[:x] =2
2

julia> a
Dict{Symbol,Int64} with 2 entries:
  :a => 3
  :x => 2

julia> a[:b] =4
4

julia> a
Dict{Symbol,Int64} with 3 entries:
  :a => 3
  :b => 4
  :x => 2

I don’t want this to happen, i want the keys to be in the order of :a,:x,:b
Thanks

4 Likes

Check out OrderedDicts from DataStructures.jl.

5 Likes

https://github.com/JuliaLang/julia/pull/10116

2 Likes

OrderedDict from DataStructures is what you want

julia> using DataStructures

julia> a = OrderedDict{Symbol,Int}()
DataStructures.OrderedDict{Symbol,Int64} with 0 entries

julia> a[:a] =3
3

julia> a
DataStructures.OrderedDict{Symbol,Int64} with 1 entry:
  :a => 3

julia> a[:x] =2
2

julia> a
DataStructures.OrderedDict{Symbol,Int64} with 2 entries:
  :a => 3
  :x => 2

julia> a[:b] =4
4

julia> a
DataStructures.OrderedDict{Symbol,Int64} with 3 entries:
  :a => 3
  :x => 2
  :b => 4
1 Like