[Python-ideas] OrderedDict.peekitem()

Eric Snow ericsnowcurrently at gmail.com
Mon Jul 6 23:59:55 CEST 2015


On Mon, Jul 6, 2015 at 3:30 PM, Neil Girdhar <mistersheik at gmail.com> wrote:
> SortedDict (http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html)
> manages to support indexing.  Can OrderedDict do the same thing?

Don't forget that, as the docs describe, an "OrderedDict is a dict
that remembers the order that keys were first inserted".  While
obviously there's an implicit sequence for that order, the focus is
still on dict-ness with the sequence exposed through the normal
mapping approach (iteration).  If you want to get sequence semantics
then first unpack the order into a sequence type like list or tuple.
Or use some other type than OrderedDict.

Note that OrderedDict's view types are essentially just dict's view
types with custom iteration.  Adding indexing to the views would
complicate things and certainly would not be O(1) like you would
expect indexing to be.

-eric


More information about the Python-ideas mailing list