# OrderedDict with duplicate items?

**URL:** <https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103>\
**Category:** New to Julia\
**Created:** [September 5, 2020, 1:30pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103 "2020-09-05T13:30:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 5, 2020, 1:30pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/1 "2020-09-05T13:30:30Z")

</div>

Hello!

Would anyone know if it is possible to make a slight rewrite, such that one can make a dict with duplicate entries in a specific order, initially set?

I see from this answer that one can make a duplicate key dict by using Base.ImmutableDict

> [@Initialising a dictionary with duplicate keys](https://discourse.julialang.org/t/initialising-a-dictionary-with-duplicate-keys/2290/3):
>
> If you, for some reason, want to have dictionary with duplicate keys you can use Base.ImmutableDict (which is however a bit tricky to use): julia\> a = Base.ImmutableDict(Base.ImmutableDict(Pair(:x, 1)), Pair(:x, 2)) Base.ImmutableDict{Symbol,Int64} with 2 entries: :x =\> 2 :x =\> 1 Note that adding a new value with the same key will override the initial definition, so julia\> a[:x] 2

But I also want to enforce a specific ordering.

Kind regards

---

<div class="post-metadata">

**Author:** ![mastrof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mastrof/32/17443_2.png) [@mastrof](https://discourse.julialang.org/u/mastrof)\
**Post date:** [September 5, 2020, 1:56pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/2 "2020-09-05T13:56:34Z")

</div>

Just out of curiosity, why do you want such a data structure?

I’m a Julia newbie, so 99.9% there are better ways to achieve what you want, but if I had to do something like that I would go with a vector of Pairs rather than an Ordered dict with duplicates, maybe something like this:

```julia
data = [Pair(:field1, 1), Pair(:field2, 2), Pair(:field1, 3)]
f(vec, field) = [x.second for x in vec if x.first==field]

```

Then `f(data, :field1)` returns `[1, 3]`.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 5, 2020, 1:59pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/3 "2020-09-05T13:59:05Z")

</div>

Hi!

It sounds very “weird” to do so I agree, the only reason for this is that I am using dicts in connection with LightXML to construct XML files. These files do not care about this when processed by a macine (it just finds the relevant tags), but since I want it to be human readable as well a specific ordering is of preference.

Thanks for your curiosity. I think using a vector of pairs rather than a dict could be a potential rewrite in the future, thanks for bringing it to my attention!

Kind regards

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [September 5, 2020, 2:00pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/4 "2020-09-05T14:00:36Z")

</div>

The [DataStructures](https://github.com/JuliaCollections/DataStructures.jl) package has quite a few different types of Dicts, including sorted Multi dicts. But it’s not clear to me what kind of order you are looking for.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 5, 2020, 2:03pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/5 "2020-09-05T14:03:22Z")

</div>

Hi!

I want this order:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/9/e96cc5a9ce8e196603eafba7262d18700c3faca2.png)

By using immutable dict I get this:

![image](https://global.discourse-cdn.com/julialang/original/3X/7/0/70174702b619b55e519f09b9ce3cd9a69037e135.png)

So I want “SavePosDouble” to be at the top, instead of the bottom in this picture.

If I could do it with an “OrderedDict” it would be nice, but if you know any ways involving dicts which allows me to force an order and duplicate tags i.e. “parameter”, I would love to hear more

Kind regards

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [September 5, 2020, 2:14pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/6 "2020-09-05T14:14:19Z")

</div>

Ok, I understand that you want the keys in given order. But there are no duplicate keys (that is, keys with multiple values) in your example. I am not aware of a dict that both allows duplicated keys and maintains insertion order. But it’s not clear what that means. What if you add a key at the beginning, then add the same key again later? Where should it go? Should it go at the end or be merged with the first entry?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 5, 2020, 2:41pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/7 "2020-09-05T14:41:55Z")

</div>

Okay I am using Dict inside of Julia. Using the base version:

```julia
Dict("a"=>1,"a"=>2)
Dict{String,Int64} with 1 entry:
  "a" => 2

```

Since I have a duplicate key “a”, I only get 1 entry. Using then:

```julia
Base.ImmutableDict("a"=>1,"a"=>2)
Base.ImmutableDict{String,Int64} with 2 entries:
  "a" => 2
  "a" => 1

```

It now gives me both entries but not in the order I specified. Using finally:

```julia
OrderedDict("a"=>1,"a"=>2)
OrderedDict{String,Int64} with 1 entry:
  "a" => 2

```

Same issue as with the first approach. I want it to return to me a dict with two entries, where “a” =\> 1 is first, then “a” =\> 2.

EDIT: Adding a key later is not possible as of now in what I want to implement. Everything is generated from scratch each time and not altered afterwards.

Kind regards

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [September 5, 2020, 9:20pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/8 "2020-09-05T21:20:45Z")

</div>

Usually dicts are used when you want fast random access with arbitrary keys. It sounds like you’re just going to iterate over the keys and values, so as suggested above a simple array of key/value pairs might be more appropriate.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 5, 2020, 9:26pm UTC](https://discourse.julialang.org/t/ordereddict-with-duplicate-items/46103/9 "2020-09-05T21:26:33Z")

</div>

I see. Will look into this when doing a rewrite later.

Kind regards
