[Tutor] checking for a condition across a list

Pijus Virketis virketis@fas.harvard.edu
Mon, 14 May 2001 01:57:15 -0400


>The simple and obvious way is just to write a reusable function:
>
>def alltrue(x, pred):
>    "Return true iff pred(y) is true for all y in x."
>    for y in x:
>        if not pred(y):
>            return 0
>    return 1
>
>Then, e.g.,
>
>>>> def isone(x):
>...     return x == 1
>...
>>>> alltrue(isone, [1,1,1,1])
>1

Thanks so much! Just one small note: the way you've written the function,
the usage should be:
>>> alltrue([1,1,1,1], isone) <-- inputs the other way around...

Oh, and one more thing. I've never before encountered the "return x ==1"
statement before. What exactly does it do?

Thank you again.

Pijus
----------------------------------------------------------------------------
-------------------
Please have a look at my weblog at www.fas.harvard.edu/~virketis.