[Python-Dev] The current dict is not an "OrderedDict"

Tim Peters tim.peters at gmail.com
Tue Nov 7 14:50:11 EST 2017


[Peter Ludemann]
> Does it matter whether the dict order after pop/delete is explicitly
> specified, or just specified that it's deterministic?

Any behavior whatsoever becomes essential after it becomes known ;-)

For example, dicts as currently ordered easily support LRU (least
recently used) purging like so:

On access:

    result = d.pop(key)
    d[key] = result

This moves `key` from wherever it was to the _most_ recently used position.

To purge the `numtopurge` least recently used keys (since traversing
the dict is always from least-recently to most-recently added):

    topurge = tuple(itertools.islice(d, numtopurge))
    for key in topurge:
        del d[key]

Is it worth guaranteeing that will always "work" (as intended)?  Not
to me, but I do have code that relies on it now - and we can count on
someone else saying it's utterly crucial ;-)


More information about the Python-Dev mailing list