On Tue, Mar 3, 2009 at 11:20 AM, Forest <list8a.for...@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/)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to