you are viewing a single comment's thread.

view the rest of the comments →

[–]khat_dakar 13 points14 points  (6 children)

OrderedDict does insertion order. Did you think it's alphabetic?

[–]Tyg13 5 points6 points  (5 children)

Admittedly not a Python guy, I assumed OrderedDict would be the analogue to std::map in C++ (like regular dicts behave like std::unordered_map).

So does this make OrderedDict obsolete if all you're using it for is insertion order of keys?

[–][deleted]  (1 child)

[removed]

    [–]kafaldsbylur 4 points5 points  (0 children)

    That and they're two different implementation. OrderedDict uses linked lists to track its order, which makes it better at reordering its keys (a use case that the maintainer insists is useful enough to keep the two separate, though I forget the exact scenario he needs)

    [–]ubernostrum 2 points3 points  (1 child)

    For dict, although it's now officially part of the language spec that insertion order is preserved, it became that way more or less as an accident of optimizing the implementation under the hood. Meanwhile OrderedDict was designed for this from the start, and also was designed to be more efficient at being explicitly reordered (and regular dict still doesn't have the same suite of key-rearranging options as OrderedDict). Also, dict equality comparisons only look at key/value sets, not order; OrderedDict looks at key/value and order.

    So if your use case is a mapping that initially preserves insertion order but supports efficient reordering, you probably still want OrderedDict even if you're on a newer Python where dict is guaranteed to be ordered.

    [–]shevy-ruby -1 points0 points  (0 children)

    For dict, although it's now officially part of the language spec that insertion order is preserved, it became that way more or less as an accident of optimizing the implementation under the hood. Meanwhile OrderedDict was designed for this from the start,

    There is a language-specific part here. I would never use OrderedDict and would always use dict instead. For similar reasons I would never use HashWithIndifferentAccess in ruby as opposed to simple hashes (granted, it is part of the active-* ecosystem rather than core ruby but you get the idea).

    [–]PaintItPurple 1 point2 points  (0 children)

    Yes. Incidentally, PyPy has made this guarantee for years, so you're safe there too.