Cleaning up conditionals

Steve D'Aprano steve+python at pearwood.info
Sat Dec 31 04:34:53 EST 2016


On Sat, 31 Dec 2016 11:26 am, Deborah Swanson wrote:

> As Mr. Bieber points out, what I had above greatly benefits from the use
> of conjunctions. It now reads:
> 
> if not len(l1[st]) and len(l2[st]):
>     l1[st] = l2[st]
> elif not len(l2[st]) and len(l1[st]):
>     l2[st] = l1[st]

Your code could do with more descriptive variable names, but keeping your
original names for the moment:

if not l1[st] and l2[st]:
    # first string is empty, second is not empty
    l1[st] = l2[st]
elif not l2[st] and l1[st]:
    # second string is empty, first is not empty
    l2[st] = l1[st]
    

The comments are, of course, redundant, and I probably wouldn't bother
putting them in my own code.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list