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
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
> 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
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
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
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(
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(
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