A Dict preserving the insertion order?


#1

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


#2

Check out OrderedDicts from DataStructures.jl.


#3

#4

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