OrderedDict with duplicate items?


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

But I also want to enforce a specific ordering.

Kind regards

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:

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].



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

The DataStructures 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.


I want this order:

By using immutable dict I get this:


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

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?

1 Like

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

Dict{String,Int64} with 1 entry:
  "a" => 2

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

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:

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

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.


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

Kind regards