any() and all() on empty list?

Ron Adam rrr at ronadam.com
Thu Mar 30 06:06:06 EST 2006


Steven D'Aprano wrote:
> Paul Rubin wrote:
> 
>> Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
>>
>>> Think of it this way: if all(seq) is true, shouldn't it be the case
>>> that you can point to a specific element in seq that is true?
>>
>>
>> No, all(seq) is true if you can't point to a specific element in seq
>> that's false.
> 
> No, all(seq) is true if every element in seq is true. Surely that's a 
> more intuitive definition than your definition by what you can't do.


Yes, they are both valid view points.  One is 'is all true' and the 
other is 'has all true'.

You can also use either to express the other...

      S and isalltrue(S)       ->  hasalltrue(S)

      not S or hasalltrue(S)   ->  isalltrue(S)



A possibly useful thing to have:

      hasall(S, value, test=None)

      hasall(S, True)         # Test for actual "True" values or 1.
      hasall(S, True, bool)   # test for true values, not zero or False.
      hasall(S, 'ok')
      hasall(S, True, lambda n: n=42)

Cheers,
    Ron










More information about the Python-list mailing list