# Cleaning up conditionals

Gregory Ewing greg.ewing at canterbury.ac.nz
Sun Jan 1 18:52:12 EST 2017

```> On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson"
> <python at deborahswanson.net> declaimed the following:
>
>>       if len(l1[v]) == 0 and len(l2[v]) != 0:
>>           l1[v] = l2[v]
>>       elif len(l2[v]) == 0 and len(l1[v]) != 0:
>>           l2[v] = l1[v]
>>       elif l1[v] != l2[v]:
>>           ret += ", " + labels[v] + " diff" if len(ret) > 0 else
>>           labels[v] + " diff"

There are a couple of things I would probably do
with this:

1. Drop the len() calls and just test the lists directly.

2. Eliminate some redundant indexing operations.

l1v = l1[v]
l2v = l2[v]
if l2v and not l1v:
l1[v] = l2v
elif l1v and not l2v:
l2[v] = l1v
elif l1v != l2v:
ret += ", " + labels[v] + " diff" if len(ret) > 0 else
labels[v] + " diff"

Jussi Piitulainen wrote:
> (l1 if bool(l1[v]) < bool(l2[v]) else
>  l2 if bool(l1[v]) > bool(l2[v]) else
>  l1)[v] = (l2 if bool(l1[v]) < bool(l2[v]) else
>            l1 if bool(l1[v]) > bool(l2[v]) else
>            l1)[v]
>

If you're aiming for readability, that's *not* the way
to go! Not every 7-line function needs to be written
on one line. :-)

--
Greg
```