|
| ▲ | mcherm 2 days ago | parent | next [-] |
| Personally, I find lots of reasons to prefer an orders Dict to an unordered one. Even small effects like "the debugging output will appear in a consistent order making it easier to compare" can be motivation enough in many use cases. |
|
| ▲ | xen0 2 days ago | parent | prev | next [-] |
| It's sometimes nice to be deterministic. I don't often care about a specific order, only that I get the same order every time. |
| |
| ▲ | no_wizard 2 days ago | parent [-] | | Thinking about this upfront for me, I am actually wondering why this is useful outside of equality comparisons. Granted, I live and work in TypeScript, where I can't `===` two objects but I could see this deterministic behavior making it easier for a language to compare two objects, especially if equality comparison is dependent on a generated hash. The other is guaranteed iteration order, if you are reliant on the index-contents relationship of an iterable, but we're talking about Dicts which are keyed, but extending this idea to List, I see this usefulness in some scenarios. Beyond that, I'm not sure it matters, but I also realize I could simply not have enough imagination at the moment to think of other benefits | | |
| ▲ | xen0 2 days ago | parent | next [-] | | I work on a build system (Bazel), so perhaps I care more than most. But maybe it does all just come down to equality comparisons. Just not always within your own code. | |
| ▲ | dontlaugh 2 days ago | parent | prev [-] | | Being able to parse something into a dict and then serialise it back to the same thing is a bit easier. Not a huge advantage, though. |
|
|
|
| ▲ | morshu9001 2 days ago | parent | prev | next [-] |
| Same. Recently I saw interview feedback where someone complained that the candidate used OrderedDict instead of the built-in dict that is now ordered, but they'll let it slide... As if writing code that will silently do different things depending on minor Python version is a good idea. |
| |
| ▲ | tehnub 2 days ago | parent | next [-] | | Well it's been guaranteed since 3.7 which came out in 2018, and 3.6 reached end-of-life in 2021, so it's been a while. I could see the advantage if you're writing code for the public (libraries, applications), but for example I know at my job my code is never going to be run with Python 3.6 or older. | | |
| ▲ | morshu9001 2 days ago | parent [-] | | Yeah, if you have that guarantee then I wouldn't fault anyone for using dict, but also wouldn't complain about OrderedDict. |
| |
| ▲ | sam_bristow a day ago | parent | prev [-] | | Honestly, if I was writing some code that depended on dicts being ordered I think I'd still use OrderedDict in modern Python. I gives the reader more information that I'm doing something slightly unusual. | | |
|
|
| ▲ | vanviegen 2 days ago | parent | prev | next [-] |
| Indeed! I don't understand why it isn't more common for stdlibs to include key-ordered maps and sets. Way more useful than insertion ordering. |
| |
| ▲ | zahlman 2 days ago | parent [-] | | Presumably because it involves different performance characteristics. |
|
|
| ▲ | kzrdude 2 days ago | parent | prev | next [-] |
| It seems like opinions really differ on this item then. I love insertion sort ordering in mappings, and python with it was a big revelation. The main reason is that keys need some order, and insertion order -> iteration order is a lot better than pseudorandom order (hash based orders). For me, it creates more reproducible programs and scripts, even simple ones. |
|
| ▲ | BiteCode_dev 2 days ago | parent | prev [-] |
| Ordering is very useful for testing. This morning for example, I tested an object serialized through a JSON API. My test data seems to never match the next run. After a while, I realized one of the objects was using a set of objects, which in the API was turned into a JSON array, but the order of said array would change depending of the initial Python VM state. 3 days ago, I used itertools.group by to group a bunch of things. But itertools.group by only works on iterable that are sorted by the grouping key. Now granted, none of those recent example are related to dicts, but dict is not a special case. And it's iterated over regularly. |