It isn't uncommon to try and get either the first or the last True value from a list. In Python 2, you'd do this:next((x for x in mylist if x))And, in Python 3, thanks to filter returning an iterator, you’d do this:
next(filter(bool,mylist))It still is pretty common. Common enough to make it aggravating to write a function to do that nearly every time. My idea is to add
firstandlastfunctions that do just that.Stuff that’s open to lots of debate:
Names. They’re not very creative; I know.
Builtin or itertools. I’m personally leaning towards the latter at the moment.
Implementing it in itertools would be simple:
def first(it): return next(filter(bool,it)) def last(it): return next(reversed(filter(bool,it)))
--RyanIf anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/