all(variable > element for element in list)
or for element i
of lists within a list
all(variable > sublist[i] for sublist in list)
This has the advantage of kicking out early if any of the elements is too large. This works because the ... for ... in list
is an instance of Python's powerful and multifarious generator expressions. They are similar to list comprehensions but only generate values as needed.
all
itself just checks to see if all of the values in the iterable it's passed are true. Generator expressions, list comprehensions, lists, tuples, and other sorts of sequences are all iterables.
So the first statement ends up being equivalent to calling a function like
def all_are_greater_than_value(list, value):
for element in list:
if element <= value:
return False
return True
with all_are_greater_than_value(list, variable)
...
or simply
all_greater = True
for element in list:
if element <= value:
all_greater = False
break
However, the generator expression is generally preferred, being more concise and "idiomatic".