# Appending to a dictionary in Julia

**URL:** <https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023>\
**Category:** New to Julia\
**Tags:** question, dictionary\
**Created:** [July 30, 2022, 7:34am UTC](https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023 "2022-07-30T07:34:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Debartha\_Paul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/debartha_paul/32/202192_2.png) [@Debartha\_Paul](https://discourse.julialang.org/u/Debartha_Paul)\
**Post date:** [July 30, 2022, 7:34am UTC](https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023/1 "2022-07-30T07:34:56Z")

</div>

I recently completed the course [Introduction to Julia (for programmers)](https://juliaacademy.com/p/intro-to-julia). However, in the lecture titled **Data Structures** , there was a part where it was shown how to add an entry to a dictionary. Now suppose we have a dictionary like the following:

```julia
myphonebook = Dict("Jenny" => "867-5309", "Ghostbusters" => "555-2368")

```

and suppose we want to add one more entry and thus, we may write it as:

```julia
myphonebook["Kramer"] = "555-FILK"

```

I expected the modified dictionary to be like this:

```julia
Dict{String, String} with 3 entries:
  "Jenny" => "867-5309"
  "Ghostbusters" => "555-2368"
  "Kramer" => "555-FILK"

```

However, in both, the lecture and on my computer, after I did the above operation, I found that my newly added element was augmented **in between** the last two elements:

```julia
Dict{String, String} with 3 entries:
  "Jenny" => "867-5309"
  "Kramer" => "555-FILK"
  "Ghostbusters" => "555-2368"

```

I would like to know why Julia is adding my entry at another location instead of just “appending” my entry at the very end.

P.S. This question might be similar to the one asked [here](https://discourse.julialang.org/t/why-dont-julia-dictionaries-preserve-order/17751).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 30, 2022, 7:48am UTC](https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023/2 "2022-07-30T07:48:32Z")

</div>

take a look [here](https://en.wikibooks.org/wiki/Introducing_Julia/Dictionaries_and_sets)

“… Because dictionaries don’t store the keys in any particular order, you might want to output the dictionary to a sorted array to obtain the items in order: …”

or try using Dictionaries

```julia
using Dictionaries

mphb = Dictionary("Jenny" => "867-5309", "Ghostbusters" => "555-2368")

julia> mphb = Dictionary("Jenny" => "867-5309", "Ghostbusters" => "555-2368")
2-element Dictionary{String, String}
    "Jenny" │ "Ghostbusters"
 "867-5309" │ "555-2368"

insert!(mphb,"Kramer", "555-FILK")

julia> mphb
3-element Dictionary{String, String}
    "Jenny" │ "Ghostbusters"
 "867-5309" │ "555-2368"
   "Kramer" │ "555-FILK"

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 30, 2022, 8:02am UTC](https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023/3 "2022-07-30T08:02:35Z")

</div>

> [@Debartha\_Paul](#):
>
> I would like to know why Julia is adding my entry at another location instead of just “appending” my entry at the very end.

A `Dict` is an unordered collection; the order the entries are printed in does not have a particular meaning. “Appending” does not really make sense in that context.

---

<div class="post-metadata">

**Author:** ![lferrant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lferrant/32/22751_2.png) [@lferrant](https://discourse.julialang.org/u/lferrant)\
**Post date:** [July 30, 2022, 8:24am UTC](https://discourse.julialang.org/t/appending-to-a-dictionary-in-julia/85023/4 "2022-07-30T08:24:59Z")

</div>

if you need dictionaries that preserve insertion order, you can use `OrderedDict` from [OrderedCollections.jl](https://github.com/JuliaCollections/OrderedCollections.jl)
