2

I have three different answers for holiday_type

holiday_type = Approved
holiday_type = Confirmed
holiday_type = both

Python Code:

 result = ['confirm','validate'] if holiday_type == 'both' else ['confirm'] if holiday_type == 'Confirmed' else ['validate']

Result:

['validate']                // Approved
['confirm']                 // Confirmed
['confirm', 'validate']     // both

I can't understand how to compile this if else statement: which one first which one second. Can you please explain how to compile this condition flow.

5
  • 2
    Why? It is not pythonic. Commented Apr 9, 2015 at 11:14
  • Off cause is this pythonic............ but I just want to know flow of this execution. because for loop execution start at the end. Commented Apr 9, 2015 at 11:16
  • Hint: result = ['confirm','validate'] if holiday_type == 'both' else (['confirm'] if holiday_type == 'Confirmed' else ['validate']) - you can easily see two nested similar statements of true_val if condition else false_val form. Commented Apr 9, 2015 at 11:16
  • I say execution flow start at the end (right to left) so in this case how is this become nested Commented Apr 9, 2015 at 11:17
  • @S͢kyD͢ream: No: there's no simple left-to-right or right-to-left evaluation order here. The evaluation order of the conditional expression true_expr if condition else false_expr is condition and then one of true_expr or false_expr is evaluated, as necessary. Commented Apr 9, 2015 at 11:24

6 Answers 6

3

Don't do this. Readability counts.

if holiday_type == 'both':
    result = ['confirm','validate']
elif  holiday_type == 'Confirmed':
    result = ['confirm']
else:
    result = ['validate']
Sign up to request clarification or add additional context in comments.

1 Comment

this doesn't answer the question (while strange use of ternary operations - is a valid cryt). Sometimes you read other peoples strange code so you need to know how it works exactly before refactoring it.
2

Statement

<expression1> if <condition> else <expression2>  

first evaluates the condition; if it returns True, expression1 will be evaluated to give the result, otherwise expression2.

In

 result = ['confirm','validate'] if holiday_type == 'both' else ['confirm'] if holiday_type == 'Confirmed' else ['validate']  

['confirm'] if holiday_type == 'Confirmed' else ['validate'] is expression2 and it will be evaluated if holiday_type == 'both' is evaluated to False.

Comments

0

This is a nested if-else

generally speaking - x if cond else y is the normal syntax or a type of statement

now here x and y can be independent set of statements

You can expand them.

Like in your case y is the same statement -> x if cond else y So it becomes

x if cond else x1 if cond2 else y1

Comments

0
result = (['confirm','validate'] if holiday_type == 'both' else
          ['confirm'] if holiday_type == 'Confirmed' else
          ['validate'])

Comments

0

If th expression is different for different cases, this would be works,

expr1 if condition1 else expr2 if condition2 else expr

For example:

>>> a = -5
>>> "negative" if a<0 else "zero" if a==0 else "positive"
'negative'
>>> a = 5
>>> "negative" if a<0 else "zero" if a==0 else "positive"
'positive'
>>> a = 0
>>> "Negative" if a<0 else "zero" if a==0 else "positive"
'zero'

Question Example:

result = ['confirm','validate'] if holiday_type == 'both' else ['confirm'] if holiday_type == 'Confirmed' else ['validate']

Comments

0

I'm going to borrow from others but give a more generic explanation of how to parse these oneline if statements by translating it to multiple lines.

expr1 if condition1 else expr2 if condition2 else expr

translates to

if condition1:
  expr1
elif condition2:
  expr2
else:
  expr

Keep in mind this same style of syntax is used for list comprehensions and other areas of python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.