Skip to content Skip to sidebar Skip to footer

Python Equivalent Of Linq All Function?

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

Solution 1:

all_match = all(test(x) for x in stuff)

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

any_match = any(test(x) for x in stuff)

Post a Comment for "Python Equivalent Of Linq All Function?"