0

I'm a looking for some sets and dicts that keep the elements in an consistent order, regardless of insertion order, runtime environment and environment variables (like the PYTHONHASHSEED variable).

The exact order is irrelevant and may vary wildly, but it's important that when such sets/dicts hold the same values/keys, that their iteration order will always be the same.

Now I'm aware I could just use a SortedList, though to my understanding it won't eliminate duplicates.
Also I know of OrderedDict, but as far as I understand all that does is preserve insertion order, which is not quite what I want.

BrainStone
  • 3,028
  • 6
  • 32
  • 59
  • do you need order to be stable within single runtime, or should it be kept after process restart? – Maciej Wrobel Jan 24 '22 at 19:15
  • using sorted containers is probably the closest to what you want. use the package `sortedcontainers` (presumably, this is what you were already talking about since you referenced a `SortedList` which is not part of the standard library....) but *don't use the list, use the set or dict* – juanpa.arrivillaga Jan 24 '22 at 19:16
  • @MaciejWrobel it needs to be consistent on every run on the program, on every computer it runs. – BrainStone Jan 24 '22 at 19:20
  • Can you give an example of how you want to use this dict/set? Are you iterating over just the keys? – Iain Shelvington Jan 24 '22 at 19:22
  • In case you weren't aware, [this is the `sortedcontainers` package I was referring to](http://www.grantjenks.com/docs/sortedcontainers/index.html). Of course, there are performance trade-offs here, but read through that link for a discussion about that. Of course, if you are only iterating over your containers a few times, then maybe just use `for sorted(my_dict_or_set)` instead of changing the container itself, if the performance characteristics are critical – juanpa.arrivillaga Jan 24 '22 at 19:24
  • @IainShelvington well for the most part I just iterate over them or modify them. The issue being that these are part of a nested data structure in my code. And while the overall order doesn't matter, it makes debugging it nearly impossible when dumping the tree causes it to vary wildly in order. Another issue is that in production it needs to be consistent across machines, or them picking up a different order will cause issues. – BrainStone Jan 24 '22 at 19:26
  • [This might solve your problem?](https://stackoverflow.com/questions/6886294/python-2-6-treemap-sorteddictionary) – Green Cloak Guy Jan 24 '22 at 19:26
  • If ordering is that important to you, then I don't see how you have any choice other than to sort the keys when you need to access them. With a `list`, you're in control of the ordering. With a `dict` or `set`, you're not, so you'll need to do it yourself. – Tim Roberts Jan 24 '22 at 19:28
  • @TimRoberts Well the exact order is irrelevant. All that matters is that the order is _consitent_. I was hoping for something less expensive than a SortedSet/SortedDict – BrainStone Jan 24 '22 at 19:39
  • @BrainStone well, no. How could it be? You have to fundamentally change the implementations. They are hash-based containers, so you shouldn't be relying on their order to begin with – juanpa.arrivillaga Jan 24 '22 at 19:39
  • @juanpa.arrivillaga that's precisely why I'm looking for something else! – BrainStone Jan 24 '22 at 19:50
  • Ok, so what's wrong with `sortedcontainers`?? – juanpa.arrivillaga Jan 24 '22 at 19:51
  • @juanpa.arrivillaga not much it seems. They work for my purpose. I just wasn't aware that there were others besides `SortedList`. The only minor complaint would be that they actually sort the objects in a specific order, which is not needed and maybe there is a faster container out there for this set of requirements. – BrainStone Jan 24 '22 at 19:54

0 Answers0