On Sat, May 23, 2020, 12:26 AM Steven D'Aprano > Obviously not all such key functions are that simple and you may need to > write a helper function, but the same applies to filter. >
I like the key function much better than the predicate. In large part that's because as soon as you say predicate, I think filter. Particularly if I care about laziness in not looking through extra items. If you wanted everything matching a predicate, using a comprehension just seems more obvious. It could be lazy with a generator comprehension, not them you need next() to get the first. Key= has the obvious parallel with sorted() and friends. Even then, a function similar to sorted() feels more general than a method on lists only. I.e. index_of(needle, haystack, key=func) I'm not sure where that would live exactly, maybe itertools. I think you were on my side in believing an import from standard library is not a huge burden. I think if such a function existed, I'd want another argument to match n != 1 results. But then it gets ugly because one index is an integer, and multiple indices are some sort of collection. So maybe one is best...
_______________________________________________ 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/W24RTDL4VHKNDMLVMRO6CB3V2AT544XP/ Code of Conduct: http://python.org/psf/codeofconduct/