[Python-ideas] OrderedDict.peekitem()

Neil Girdhar mistersheik at gmail.com
Mon Jul 6 23:30:36 CEST 2015


SortedDict 
(http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html) manages 
to support indexing.  Can OrderedDict do the same thing?

On Monday, July 6, 2015 at 10:49:44 AM UTC-4, Kale Kundert 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)) 
>
> _______________________________________________ 
> Python-ideas mailing list 
> Python... at python.org <javascript:> 
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150706/a7ae535c/attachment.html>


More information about the Python-ideas mailing list