Re: "and" and "or" on every item in a list

2007-10-29 Thread GHZ
Thanks for the answers, I suspected something like any(), all() existed. Also got me thinking about generator expressions, my code is full of list comprehensions for lists I don't keep. -- http://mail.python.org/mailman/listinfo/python-list

Re: "and" and "or" on every item in a list

2007-10-29 Thread Dustan
On Oct 29, 5:57 pm, GHZ <[EMAIL PROTECTED]> wrote: > Is this the best way to test every item in a list? No. The biggest problem is, obviously, you don't take advantage of builtins any() and all(), or write corresponding short-circuiting versions for python versions before 2.5. The second problem

Re: "and" and "or" on every item in a list

2007-10-29 Thread Tim Chase
> 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)) > alltrue(lambda x:x>1,[1,2,3]) > False alltrue(lambda x:x>=1,[1,2,3]) > True As of Python2.5, there's

Re: "and" and "or" on every item in a list

2007-10-29 Thread Paul Hankin
On Oct 29, 10:57 pm, GHZ <[EMAIL PROTECTED]> 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 exa

Re: "and" and "or" on every item in a list

2007-10-29 Thread Carl Banks
On Oct 29, 6:57 pm, GHZ <[EMAIL PROTECTED]> 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)) Probably not, because it doesn't take advantage of short circuiti

Re: "and" and "or" on every item in a list

2007-10-29 Thread Matimus
On Oct 29, 3:57 pm, GHZ <[EMAIL PROTECTED]> 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)) > > > > >>> alltrue(lambda x:x>1,[1,2,3]) > False > > >>> alltrue(

Re: "and" and "or" on every item in a list

2007-10-29 Thread Raymond Hettinger
On Oct 29, 3:57 pm, GHZ <[EMAIL PROTECTED]> 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)) > > > > >>> alltrue(lambda x:x>1,[1,2,3]) > False > > >>> alltrue(

"and" and "or" on every item in a list

2007-10-29 Thread GHZ
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)) >>> alltrue(lambda x:x>1,[1,2,3]) False >>> >>> alltrue(lambda x:x>=1,[1,2,3]) True >>> Thanks -- http://mail.python.org/mai