Arithmetic with Boolean values

Roy Smith roy at
Sun Aug 12 13:40:30 CEST 2012

In article <502791ea$0$29978$c3e8da3$5496439d at>,
 Steven D'Aprano <steve+comp.lang.python at> wrote:

> for x in (0,) if len(L)%2 else (0, 1):
>     ...
> which is even more explicit and simpler to read even though it is longer.


if len(L) % 2 == 0:
   do_stuff()  # reprocess even-length list

Sure, it's 3 lines instead of one, but dead-obvious what the intention 
is.  I might even go for:

if len(L) % 2:

There's two problems with all the looping suggestions people have given.  
One is that the computation of whether you need to do it once or twice 
is messy.  But, but bigger issue is you're trying to warp what's 
fundamentally a boolean value into a looping construct.  That's a 
cognitive mismatch.

More information about the Python-list mailing list