[Python-ideas] Function to return first(or last) true value from list

Chris Angelico rosuav at gmail.com
Sat Feb 22 18:00:59 CET 2014


On Sun, Feb 23, 2014 at 3:52 AM, אלעזר <elazarg at gmail.com> wrote:
> If an iterators were indexable, with non-decreasing order only, then
> first(it) would become a simple it[0]. with exactly the same semantics,
> except the possibilty of raising a (visible) IndexError on an exhausted
> iterator, instead of .
>
> By "non decreasing" I mean the ability to do something like this:
>
>     it = iter('abcd')
>     for i in range(4):
>         print(it[i])
>
> Of course, it will force the iterator to have additional memory.
> I am sure this was suggested before, probably many times (and obviously
> rejected), but not exactly in this specific context.
> So, maybe allowing only it[0], or maybe generator[0], would be nice (even if
> it's just an ugly special case").

You can get that with itertools.tee(), or list().

ChrisA


More information about the Python-ideas mailing list