Bottom Page

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 ternary operator with unpacking
#1
Hello,
After using ternary operators extensively I am getting lost in this case. It took me a couple of hours to understand why I was getting bugged, and though I've identified the cause I can't understand why this example works like this.

# pretty clear
a = [("1", "2"), ("", "")]
b, c = list(zip(*a))
print(b)
print(c)
('1', '')
('2', '')
# here I would expect the same as before, however...
a = [("1", "2"), ("", "")]
b, c = list(zip(*a)) if True else [""], [""]
print(b)
print(c)
[('1', ''), ('2', '')]
['']
# this result is expected but why it differs from the second?
a = [("1", "2"), ("", "")]
b, c = list(zip(*a)) if True else ([""], [""])
print(b)
print(c)
('1', '')
('2', '')
Would someone be willing to comment on this? Smile

EDIT: after posting the thread and staring at it, a spark came... ternary operator ended with the comma... so [""] was passed to c even when condition evaluates to True
Quote
#2
Note that the result of this expression list(zip(*a)) if True else [""] will always be [the result of] first part - list(zip(*a)) because your condition is explicit True, i.e. you are not evaluating the truthfulness of the first part. More over with given value of a the first part will be considered True in any case.

In the second example, on the right hand side you have implicit tuple with first element being list(zip(*a)) if True else [""] and the second element is [""]. During the unpacking they are assigned to b and c respectively.

In the third example the right hand side ultimately ends being list(zip(*a)) - i.e. the first part of the ternary operator as explained at the top of this post. And it's unpacked accordingly into b and c.
Quote
#3
Yes, the CONDITION being True is just for example. I didn't mean to say I explicitly write True there.
Yes, then I realised that in the ternary expression giving "," ends it and unpacks differently.
I will correct my examples to avoid confusion.
Quote
#4
(Jan-07-2019, 12:26 PM)joaomcteixeira Wrote: Yes, the CONDITION being True is just for example. I didn't mean to say I explicitly write True there.
Sorry, I misunderstood that part
Quote
#5
Just for fun:

Guido van Rossum in Python-Dev list

Quote:Pleas stop calling it 'ternary expression'. That doesn't explain
what it means. It's as if we were to refer to the + operator as
'binary expression'

According to PEP308 Python has conditional expression :-)
buran likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Quote
#6
Jeje,
Thanks for sharing the info and the links Cool , let's go for conditional expression.
Best
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AND Boolean operator syntax error jsb83 3 212 May-31-2019, 08:34 AM
Last Post: perfringo
  SyntaxError: can't assign to operator Jomy10 3 541 Mar-02-2019, 12:39 PM
Last Post: Jomy10
  Problem with comparison operator mdrisser 2 143 Feb-28-2019, 09:45 PM
Last Post: mdrisser
  How I can overload operator [] ? AlekseyPython 3 248 Feb-20-2019, 05:38 AM
Last Post: AlekseyPython
  remainder operator % noweare 3 171 Feb-14-2019, 01:50 AM
Last Post: noweare
  Tuple Unpacking HarshaliPatel 3 276 Jan-30-2019, 12:42 PM
Last Post: dukoolsharma
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 205 Jan-29-2019, 02:19 PM
Last Post: perfringo
  unexpected sub result after overloading operator jolinchewjb 1 232 Jan-24-2019, 08:23 AM
Last Post: buran
  ' | ' Operator and 'OR' blackknite 10 581 Jan-11-2019, 09:32 PM
Last Post: blackknite
  How many variables/values are limited to single assignment operator? Prabakaran141 1 375 Sep-06-2018, 03:32 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)