Cleaning up conditionals
Chris Angelico
rosuav at gmail.com
Sun Jan 1 00:39:27 EST 2017
On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson
<python at deborahswanson.net> 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
More information about the Python-list
mailing list