[Tutor] Re: [Edu-sig] style question

Michael P. Reilly arcege@speakeasy.net
Wed, 20 Feb 2002 18:01:04 -0500

On Wed, Feb 20, 2002 at 01:59:13PM -0800, Kirby Urner wrote:
> >  Which (if either) of the two
> >codes would be preferred:
> I agree with Danny that a list-based approach would be
> better:
>    from operator import add
>    def check(testlist):
>        return reduce(add,[i%2==0 for i in testlist])
>    >>> check([1,4,5,9,3,8,7])
>    2

However, it is still a "bad idea" to add boolean values.  A true integer
is anything that is not zero.  Conceivably, this could be -1;  I could
easily create a __eq__ method (in Py2.1 and later) that returns -1 or
1e10 or even "true".

What's better is to get the length of the list where the value is true.

how_many_evens = len( filter(lambda i, i%2==0, testlist) )