Cleaning up conditionals

Deborah Swanson python at deborahswanson.net
Sat Dec 31 22:58:36 EST 2016


Chris Angelico wrote:
> Sent: Saturday, December 31, 2016 2:19 PM
> 
> On Sun, Jan 1, 2017 at 9:03 AM, Deborah Swanson 
> <python at deborahswanson.net> wrote:
> > And didn't finish it because I couldn't see what a should 
> be. I want 
> > it to be l2[v] if the first clause is true, and l1[v] if 
> the second. 
> > If I was computing a value, this would work beautifully, 
> but I don't 
> > see how it can if I'm choosing a list element to assign to. Maybe I 
> > just can't see it.
> 
> It's possible to select either l1 or l2 using an expression, 
> and then subscript that with [v]. However, this does not 
> usually make for readable code, so I don't recommend it.
> 
> (l1 if whatever else l2)[v] = new_value
> 
> ChrisA

I'm not sure I understand what you did here, at least not well enough to
try it.

What conditional can I do between the 2 rows of listings (the list names
l1 and l2) that will give me which row has the value to copy from and
which one is empty? I don't see how that can happen if you don't give
the subscript [v] to each of l1 and l2, at a minimum. Unless python will
distribute the [v] inside the preceding conditional?  Amazing, if true,
but then we still need what the conditional is.

And what is new_value? It could be either l1[v] or l2[v], depending on
which one is not empty, and I don't see how the answer magically pops
into it.  Not saying that it doesn't, but what should I call new_value
in the real estate listings example I want to use it in?  There are no
new values in this situation, only values that need to be copied into
their empty counterparts in the other row.  (It may or may not help to
read the synopsis of what I'm doing that I wrote up in my last post to
Peter Otten.)

No, it's not terribly readable, but I'm curious whether this actually
works.

D.




More information about the Python-list mailing list