> Except many iterables don’t have a last item. And many more can’t give
you the last item efficiently.

That's manageable - reversed won't work either unless the object either
implements either __reversed__, or __len__ and __getitem__. last could
simply fail under the same conditions, in which case you could use
last(list(obj)).

I think first, second and last, with optional default parameters, would be
great additions to itertools. I use the toolz library's first and last
functions frequently. The latter is not "smart" in the way described above
and just consumes the iterable, possibly indefinitely.

Alex

On Wed, Oct 6, 2021 at 10:46 AM Christopher Barker <python...@gmail.com>
wrote:

>
>
>>    - dict.first_key = lambda self: next(iter(self))
>>    - dict.first_val = lambda self: next(iter(self.values()))
>>    - dict.first_item = lambda self: next(iter(self.items()))
>>    - dict.last_key = lambda self: next(reversed(self))
>>    - dict.last_val = lambda self: next(reversed(self.values()))
>>    - dict.last_item = lambda self: next(reversed(self.items()))
>>
>> But I think I like a lot more the idea of adding general ways of doing
>> these things to itertools.
>>
>
> Except many iterables don’t have a last item. And many more can’t give you
> the last item efficiently.
>
> -CHB
>
>
> On 5 Oct 2021, at 05:30, Christopher Barker <python...@gmail.com> wrote:
>>
>> 
>>
>> On Mon, Oct 4, 2021 at 5:46 PM Erik Demaine <edema...@mit.edu> wrote:
>>
>>> Have folks thought about allowing indexing dictionary views as in the
>>> following code, where d is a dict object?
>>>
>>> d.keys()[0]
>>> d.keys()[-1]
>>> d.values()[0]
>>> d.values()[-1]
>>> d.items()[0]
>>> d.items()[-1]  # item that would be returned by d.popitem()
>>>
>>
>> since dicts were made order-preserving, indexing the keys, items, etc
>> does make some sense.
>>
>>
>> > I've also often wanted to get an arbitrary item/key from a dictionary,
>> and
>>
>> This is indeed one of the use cases identified.
>>
>> I found some related discussion in
>>>
>>> https://mail.python.org/archives/list/python-ideas@python.org/thread/QVTGZD6USSC34D4IJG76UPKZRXBBB4MM/
>>> but not this exact idea.
>>>
>>
>> That's a pretty different idea but this exact idea has been discussed on
>> this list relatively recently. I still like it, but there wan't much
>> general support.
>>
>> I'll leave it exercise for the read to find that thead, but it is there,
>> and I suggest you look for it if you want to further pursue this idea.
>>
>> -CHB
>>
>>
>> --
>> Christopher Barker, PhD (Chris)
>>
>> Python Language Consulting
>>   - Teaching
>>   - Scientific Software Development
>>   - Desktop GUI and Web Development
>>   - wxPython, numpy, scipy, Cython
>>
>> _______________________________________________
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/RAEDZPUTNABJLX3ESU32PZQBJ25DDOPK/
>>
>>
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> --
> Christopher Barker, PhD (Chris)
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/THRXG26D74UFRXJOLZKL2CZUZDNJ6PIH/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SMTDZMCSGB65PIMGUL67EAIXY37DPVXC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to