Cleaning up conditionals

Deborah Swanson python at
Sun Jan 1 18:12:24 EST 2017

Chris Angelico wrote on Saturday, December 31, 2016 9:39 PM
> On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson 
> <python at> wrote:
> > 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.)
> Start with this simple statement:
> foo[2] = "spam"
> Obviously this will subscript 'foo' with 2 and set that to 
> the string "spam". Easy. But instead of the word 'foo', we 
> could use any expression at all.
> def return_foo():
>     return foo
> return_foo()[2] = "spam"
> This is a little more surprising, but it does work.
> And if you can use a function call to provide the list, you 
> can use anything else - even a conditional expression.
> (foo if True else [])[2] = "spam"
> It's perfectly legal, but I do NOT recommend it :)
> ChrisA

I see what you're doing here and I have applied functions to values and
variables before, so I know it's legal and that it works. Thanks for
giving me this other way that the same principle can be used with

I know I keep repeating myself, but aside from the advisability (or not)
of using this form, the real problem is that "spam" will only be the
right answer in one of the two cases.  The job of this conditional is to
choose which value to put into which field.

More information about the Python-list mailing list