Cleaning up conditionals

Deborah Swanson python at
Fri Dec 30 16:20:15 EST 2016

I've already learned one neat trick to collapse a conditional:

	a = expression1 if condition else expression2

Here I have a real mess, in my opinion:

	if len(l1[st]) == 0:
          if len(l2[st]) > 0:
              l1[st] = l2[st]
          elif len(l2[st]) == 0:
              if len(l1[st]) > 0:
                  l2[st] = l1[st]

  (Basically, if one field from two adjacent rows is empty and the other
  not, copy the non-empty field to the empty one. I use this for rental 
  listings that are identical but posted on different dates, to copy the

  data from an older one to the new one. Or, if I look up the data for a
  listing, to copy it back to the older ones.)

Anybody know or see an easier (more pythonic) way to do this? I need to
do it for four fields, and needless to say, that's a really long block
of ugly code.

Any other insights into tightening up complex conditionals?

Thanks in advance,

More information about the Python-list mailing list