[Python-ideas] `OrderedDict.items().__getitem__`

Ryan Gonzalez rymg19 at gmail.com
Sun Jan 12 00:01:57 CET 2014


It is pretty inefficient. As for getting the last item, however, I think
something like that might end up the best.

And, you've gotta admit, it isn't bad for a 30-second solution with no real
planning whatsoever.


On Sat, Jan 11, 2014 at 4:03 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Jan 12, 2014 at 8:47 AM, Mathias Panzenböck
> <grosser.meister.morti at gmx.net> wrote:
> > Why not:
> >
> >         get_first = lambda d: next(iter(d.items()))
> >
> > No need for a full copy of the dict.
> >
> >
> > On 01/11/2014 09:51 PM, Ryan Gonzalez wrote:
> >>
> >> Based on your popitem idea:
> >>
> >> get_first = lambda d: d.copy().popitem()
> >> get_last = lambda d: d.copy().popitem(last=True)
>
> Oh right. Yeah, copy(). So this isn't destructive, but as Mathias
> says, it's probably inefficient. (I say "probably" because it's
> theoretically possible to optimize the copy operation - but I don't
> see anything like that in the source code.)
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Ryan
When your hammer is C++, everything begins to look like a thumb.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140111/ce458e60/attachment.html>


More information about the Python-ideas mailing list