[Python-ideas] OrderedDict.peekitem()

Kale Kundert kale at thekunderts.net
Mon Jul 6 16:39:58 CEST 2015


Today I was trying to use collections.OrderedDict to manage a LIFO queue, and I
was surprised to realize that OrderedDict doesn't provide a way to look at its
first or last item.  There is an OrderedDict.popitem() method, which removes and
returns either the first or last item, but it's not hard to imagine cases where
you would want to see what's on the queue without popping it right away.

My proposal is to add a peekitem() method to OrderedDict.  This method would
have the same signature and would return the same thing as popitem(), it just
wouldn't modify the data structure.

-Kale

P.S. There is already a way to peek at the last item an OrderedDict, but it
hides the intent of the code and you wouldn't think of it if you weren't
familiar with python: next(reversed(ordered_dict))



More information about the Python-ideas mailing list