<div dir="ltr">Based on your popitem idea:<br><br><div>get_first = lambda d: d.copy().popitem()</div><div>get_last = lambda d: d.copy().popitem(last=True)</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sat, Jan 11, 2014 at 8:36 AM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Sun, Jan 12, 2014 at 1:18 AM, Ram Rachum <<a href="mailto:ram.rachum@gmail.com">ram.rachum@gmail.com</a>> wrote:<br>
> I think that `OrderedDict.items().__getitem__` should be implemented, to<br>
> solve this ugliness:<br>
><br>
> <a href="http://stackoverflow.com/questions/21062781/shortest-way-to-get-first-item-of-ordereddict-in-python-3" target="_blank">http://stackoverflow.com/questions/21062781/shortest-way-to-get-first-item-of-ordereddict-in-python-3</a><br>
><br>
> What do you think?<br>
<br>
</div></div>Well, the first problem with that is that __getitem__ already exists,<br>
and it's dict-style :) So you can't fetch out an item by its position<br>
that way. But suppose you create a method that returns the Nth<br>
element.<br>
<br>
The implementation in CPython 3.4 is a linked list, so getting an<br>
arbitrary element by index would be quite inefficient. Getting<br>
specifically the first can be done either with what you see in that<br>
link (it could be made a tiny bit shorter, but not much), but anything<br>
else would effectively entail iterating over the whole thing until you<br>
get to that position, so you may as well do that explicitly.<br>
Alternatively, if you're okay with it being a destructive operation,<br>
you can use popitem() to snag the first (or last, if you wish)<br>
key/value pair.<br>
<br>
ChrisA<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Ryan<div><span style="color:rgb(0,0,0);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;line-height:18px"><font>When your hammer is C++, everything begins to look like a thumb.</font></span><br>
</div></div>
</div>