any() and all() on empty list?

Ron Adam rrr at ronadam.com
Sat Apr 1 03:06:29 EST 2006


Steve R. Hastings wrote:
> On Fri, 31 Mar 2006 16:29:00 -0800, Paul Rubin wrote:
>> I think "S and all(S)" is the right way to express that, if that's
>> what's intended.
> 
> I still would like a standard function, because "S and all(S)" does not
> work with iterators.  I proposed one possible function, truecount(S), that
> returns a tuple of how many were true and how many there were total.  Then
> you could do
> 
> true_count, count = truecount(S)
> 
> if count and true_count == count:
>   # nonempty list and all are true
> 
> 
> And S could be an iterator or generator function expression.
> 
> You can easily write your own truecount() but it would be nice to have
> something like that as standard.  I don't much like the name "truecount"
> though; I'm open to suggestions for a better name.

How about:

      countall(S, value=True)


Considering len() is used to get a length,  and countall() is related to 
all(), but it's explicit about what it's counting and would not return 
True on an empty set.  I think it would be useful.

     true_count, count = countall(S), len(S)

Cheers,
    Ron




More information about the Python-list mailing list