top | item 41453180

(no title)

padthai | 1 year ago

Why do you use OrderedDict for now that regular dicts are ordered by default?

discuss

order

Flimm|1 year ago

Two dictionaries with equal keys and values are considered equal in Python, even if the order of the entries differ. By contrast, two OrderedDict objects are only equal if their respective entries are equal and if their order does not differ.

d0mine|1 year ago

It may be more explicit: OrderedDict has move_to_end() which may be useful e.g., for implementing lru_cache-like functionality (like deque.rotate but with arbitrary keys).

masklinn|1 year ago

OTOH that’s a lot less useful now that functools.lru_cache exists: it’s more specialised so it’s lighter, more efficient, and thread-safe. So unless you have extended flexibility requirements around your LRU, OD loses a lot there.

And if you’re using a FIFO cache, threading a regular dict through a separate fifo (whether linked list or deque) is more efficient in my experience of implementing both S3 and Sieve.

heavyset_go|1 year ago

OrderedDicts have some convenience methods and features that ordinary dicts don't have.

wodenokoto|1 year ago

Do you have any examples?

rbanffy|1 year ago

Also, dicts can become unordered at any time in the future. Right now the OrderedDict implementation is a thin layer over dict, but there are no guarantees it’ll always be that.

judicious|1 year ago

I work with different versions of Python3 (and 2 unfortunately) and some code is still in 3.6, hence I used OrderedDicts.

mixmastamyk|1 year ago

3.6 was the first with the new ordered by default dicts, even though wasn't specc'd until 3.7.

sgarland|1 year ago

As someone who just had to backport a fairly large script to support 3.6, I found myself surprised at how much had changed. Dataclasses? Nope. `__future__.annotations`? Nope. `namedtuple.defaults`? Nope.

It's also been frustrating with the lack of tooling support. I mean, I get it – it's hideously EOL'd – but I can't use Poetry, uv, pytest... at least it still has type hints.