[Python-ideas] OrderedDict.peekitem()

Todd toddrjen at gmail.com
Mon Jul 6 16:56:22 CEST 2015


On Jul 6, 2015 4:49 PM, "Kale Kundert" <kale at thekunderts.net> wrote:
>
> 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))

What about just making OrderedDict.values support indexing?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150706/b3a0c985/attachment.html>


More information about the Python-ideas mailing list