# A Dict preserving the insertion order?

**URL:** <https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124>\
**Category:** General Usage\
**Created:** [February 17, 2018, 8:06am UTC](https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124 "2018-02-17T08:06:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [February 17, 2018, 8:06am UTC](https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124/1 "2018-02-17T08:06:56Z")

</div>

Is there a dictionary which preserves the adding order.

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

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [February 17, 2018, 8:24am UTC](https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124/2 "2018-02-17T08:24:48Z")

</div>

Check out [OrderedDicts](http://juliacollections.github.io/DataStructures.jl/latest/ordered_containers.html) from [DataStructures.jl](https://github.com/JuliaCollections/DataStructures.jl).

---

<div class="post-metadata">

**Author:** ![samoconnor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samoconnor/32/1802_2.png) [@samoconnor](https://discourse.julialang.org/u/samoconnor)\
**Post date:** [February 17, 2018, 8:27am UTC](https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124/3 "2018-02-17T08:27:47Z")

</div>

[https://github.com/JuliaLang/julia/pull/10116](https://github.com/JuliaLang/julia/pull/10116)

---

<div class="post-metadata">

**Author:** ![drvi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvi/32/13236_2.png) [@drvi](https://discourse.julialang.org/u/drvi)\
**Post date:** [February 17, 2018, 9:25am UTC](https://discourse.julialang.org/t/a-dict-preserving-the-insertion-order/9124/4 "2018-02-17T09:25:16Z")

</div>

OrderedDict from DataStructures is what you want

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

```
