# Cleaning up conditionals

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Jan 2 04:44:09 EST 2017

```Deborah Swanson writes:
> Jussi Piitulainen wrote:

[snip]

>> With your particular conditions of non-emptiness, which is taken to
>> be truth, you can achieve variations of this result with any of the
>> following statements:
>>
>> w = ( l1[v] if len(l1[v]) > 0 else
>>       l2[v] if len(l2[v]) > 0 else
>>       l1[v] )
>>
>> x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v]
>>
>> y = l1[v] or l2[v] or l1[v]
>>
>> z = l1[v] or l2[v]
>>
>> The last one, which I originally suggested (and still prefer
>> when otherwise appropriate), is subtly different from the
>> others. That difference should be irrelevant.
>
> I agree, if the goal was to capture one of the field values in a
> scalar value.

To store into a list, specify a position in the list as the target.

My idea here has been to simply do this to all the relevant positions in
both lists, even when it means storing the old value back.

See below, concretely, with your two examples and the mixed one from
Dennis Lee Bieber, where I introduced a small difference of my own so
that corresponding non-empty fields differ. I have made it output Python
comments and inserted them at appropriate places.

The same function, merge, fills the empty fields from the other list in
all three cases using the method z from above. It does no harm when a
field is already non-empty.

def merge(l1, l2):
fields = range(5)
for v in fields:
l1[v] = l1[v] or l2[v]
l2[v] = l2[v] or l1[v]

l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d' ]
l2 = [ '2 br, Elk Plains', '12-29', '',      '',      ''            ]

print('# Before:', l1, l2, sep = '\n# ', end = '\n# ')
merge(l1, l2)
print('After:', l1, l2, sep = '\n# ', end = '\n\n')

# Before:
# ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d']
# ['2 br, Elk Plains', '12-29', '', '', '']
# After:
# ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d']
# ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']

l1 = [ '2 br, Elk Plains', '12-26', '',      '',      ''            ]
l2 = [ '2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d' ]

print('# Before:', l1, l2, sep = '\n# ', end = '\n# ')
merge(l1, l2)
print('After:', l1, l2, sep = '\n# ', end = '\n\n')

# Before:
# ['2 br, Elk Plains', '12-26', '', '', '']
# ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']
# After:
# ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d']
# ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']

l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', '',      ''            ]
l2 = [ '2 br, Elf Plains', '12-29', '',      'house', 'garage, w/d' ]

print('# Before:', l1, l2, sep = '\n# ', end = '\n# ')
merge(l1, l2)
print('After:', l1, l2, sep = '\n# ', end = '\n\n')

# Before:
# ['2 br, Elk Plains', '12-26', 'WA/pi', '', '']
# ['2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d']
# After:
# ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d']
# ['2 br, Elf Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']
```