Arithmetic with Boolean values

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Aug 12 07:22:19 EDT 2012


On Sat, 11 Aug 2012 17:54:40 -0700, Paul Rubin wrote:

> John Ladasky <john_ladasky at sbcglobal.net> writes:
[...]
>> If the length of the list L is odd, I want to process it once.  If
>> len(L) is even, I want to process it twice....
>>   for x in range(1 + not(len(L) % 2)):
> 
> If you really have to do something like that, I'd say
> 
>    for x in range(1 + (len(L) & 1)):
[snip]

I'd simplify it even more:

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.



-- 
Steven



More information about the Python-list mailing list