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

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Jan 11 23:32:02 CET 2014


On 11/01/2014 22:03, Chris Angelico 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
>

Surely a shallow copy isn't guaranteed to work properly in all cases anyway?

copy(...)
      D.copy() -> a shallow copy of D

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Python-ideas mailing list