[Python-ideas] OrderedDict.peekitem()

Neil Girdhar mistersheik at gmail.com
Tue Jul 7 10:09:20 CEST 2015


Good points.  I will do my best to address them:

3. The reason I like "next(iter(" and "reversed(iter", which is also
probably the reason the discussion drifted into indexing is that right
after someone asks for peek, someone else asks for peek_next and so on.  If
you're unhappy with logarithmic indexing time as you suggested, then the
best you can do is linear time with respect to peek depth.  That is just
iteration.   The most readable way to express one step of iteration is to
call next on an iterator.  The most readable way to produce an iterator
from an iterable is with iter or reversed.

1/2. If you don't have a good understanding of Python, I think that that is
something that should be remedied by learning about Python?  There should
ideally be one way of doing things after all.

Maybe the documentation could be extended with a mention of how to peek?  I
don't know.  But I disagree with adding peek to OrderedDict for the same
reason that I disagree with adding peek to list and to heapq (as
"heapq.peek(h)").

Best,

Neil

On Tue, Jul 7, 2015 at 3:59 AM, Kale Kundert <kale at thekunderts.net> wrote:

> Three things:
>
> 1. Someone who is not all that familiar with python would never think to do
> either of those things.  I've been using python for years and it didn't
> occur to
> me that I could peek at the ends of an OrderedDict like that until I saw
> someone
> on stack overflow suggest it.
>
> 2. Furthermore, if you don't have a good understanding of iterators in
> python,
> you could be excused for thinking that 'next(reversed(o))' creates a
> temporary
> list and is O(n) in time and memory.  Those expressions read like they're
> doing
> a lot more work than they actually are, and that's confusing.
>
> 3. Readability counts, and those expressions hide the intent of the
> programmer.
>  You wouldn't use 'next(iter(o))' to access the first element of a list,
> because
> that would be confusing and obfuscated.
>
> On 07/06/2015 11:59 PM, Neil Girdhar wrote:
> > What's wrong with "next(iter(o))" and "next(reversed(o))"?
> >
> > On Tue, Jul 7, 2015 at 2:56 AM, Kale Kundert <kale at thekunderts.net
> > <mailto:kale at thekunderts.net>> wrote:
> >
> >     I didn't even mean for this thread to be about arbitrarily indexing
> into an
> >     OrderedDict.  I meant for it to be about accessing the first and
> last items in
> >     an OrderedDict.  Given that a method already exists to access and
> remove these
> >     items, I find it hard to understand why there isn't a method to
> simply access
> >     them.  This should be a constant-time operation if OrderedDict
> employs a
> >     doubly-linked list under the hood.
> >
> >     -Kale
> >
> >     On 07/06/2015 09:23 PM, Neil Girdhar wrote:
> >     > This thread is not about hash tables.  This thread is about
> indexing into an
> >     > ordered dictionary when you need an ordered dictionary.  Someone
> pointed out
> >     > that people expect indexing to be constant time.  I agree that no
> one expects
> >     > indexing to be linear time.  My point was that logarithmic-time
> indexing is
> >     > reasonable and possible.
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150707/2ede46d0/attachment.html>


More information about the Python-ideas mailing list