"and" and "or" on every item in a list
paul.hankin at gmail.com
Tue Oct 30 00:24:44 CET 2007
On Oct 29, 10:57 pm, GHZ <geraint.willi... at gmail.com> wrote:
> Is this the best way to test every item in a list?
> def alltrue(f,l):
> return reduce(bool.__and__,map(f,l))
> def onetrue(f,l):
> return reduce(bool.__or__,map(f,l))
No. In Python 2.5 there are builtins 'all' and 'any' that do exactly
these. If you're using python <2.5, then you can support shortcut
evaluation - there's no need to check every element once you find a
false in 'all' or a true in 'any'.
More information about the Python-list