[Python-Dev] PEP 372 -- Adding an ordered directory to collections ready for pronouncement

Guido van Rossum guido at python.org
Tue Mar 3 20:54:18 CET 2009


On Tue, Mar 3, 2009 at 11:20 AM, Forest <list8a.forest at tibit.com> wrote:
> I'm looking forward to an ordered dictionary in the standard library,
> especially for things like LRU caches.  I was just reading the PEP, and
> caught this bit:
>
> "Does OrderedDict.popitem() return a particular key/value pair?
> Yes. It pops-off the most recently inserted new key and its corresponding
> value."
>
> Okay, but I'd also like a convenient and fast way to find the oldest entry
> in an OrderedDict, which I think I'd need for an LRU cache.  Skimming the
> current patch (od7.diff), I didn't notice one.  Perhaps I simply missed
> something.

What's your use case?

If you really need this you can do it easily by taking the first key
returned by the iterator:

def popfirstitem(o):
  key = next(iter(o))  # raises StopIteration if o is empty
  value = o[key]
  del o[key]
  return key, value

> Shouldn't popitem() allow the caller to choose which end from
> which to pop?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list