Is this an effective and Putin way to see if any element in the iterable is true for a particular attribute?

Say you have an iterative sequence of objects thingcalled things. Each thinghas a method is_whatever()that returns True if it fulfills the "all" criteria. I want to effectively find out if there is any item in things.

Here is what I am doing now:

any_item_is_whatever = True in (item.is_whatever() for item in items)

This is an effective way to do this, i.e. Will Python stop generating elements from iterable as soon as it finds the first True result? Stylistic, is it pythonic?

+5
source share
1 answer

You must use the built-in function any():

any_item_is_whatever = any(item.is_whatever() for item in items)
+12
source

All Articles