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

Peter Otten __peter__ at web.de
Sat Jan 11 16:36:49 CET 2014


Ram Rachum wrote:

> I think that `OrderedDict.items().__getitem__` should be implemented, to
> solve this ugliness:
> 
> http://stackoverflow.com/questions/21062781/shortest-way-to-get-first-
item-of-ordereddict-in-python-3
> 
> What do you think?

I think an O(N) __getitem__() is even uglier. Also, you should have really 
compelling reasons for allowing the interfaces of dict.items() and 
OrderedDict.items() to diverge.

Personally, I'd use a helper function

def first(items):
    for item in items:
        return item
    raise ValueError("No first item in an empty sequence")

and I don't understand why user thefourtheye is downvoted. Hiding a non-
obvious if small piece of code behind a self-explaining name seems like good 
programming practice.



More information about the Python-ideas mailing list