4 ms·
The itertools documentation¹ include a recipe to do just that, and links to the excellent more-itertools² package which bundles up a heap of recipes that make i
by JNRowe 5y ago
The itertools documentation¹ include a recipe to do just that, and links to the excellent more-itertools² package which bundles up a heap of recipes that make iterators even better. This specific case is handled by first_true² in more-itertools.
¹ https://docs.python.org/3/library/itertools.html#itertools-recipes https://docs.python.org/3/library/itertools.html#itertools-r...
² https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.first_true https://more-itertools.readthedocs.io/en/stable/api.html#mor...
- memco 5y agoI think the first_true optio is great and I highly recommend more-itertools, but if you’re looking for a built in alternative I usually use next((I for I in iter if is_thing(I)), default) It’s slightly more work than any but is pretty much all I need when I need to handle whatever values are returned from the any.