What is the idiomatic Python way to test if all elements in a collection satisfy a condition? (The .NET All() method fills this niche nicely in C#.)
There’s the obvious loop method:
all_match = True
for x in stuff:
if not test(x):
all_match = False
break
And a list comprehension could do the trick, but seems wasteful:
all_match = len([ False for x in stuff if not test(x) ]) > 0
There’s got to be something more elegant… What am I missing?
This short-circuits and doesn’t require stuff to be a list — anything iterable will work — so has several nice features.
There’s also the analogous