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

Chris Angelico rosuav at gmail.com
Sat Jan 11 23:03:39 CET 2014


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


More information about the Python-ideas mailing list