Jaykumar Patel
Jaykumar Patel

Reputation: 27614

How to check single line if else if else condition in Python

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.

Upvotes: 2

Views: 1527

Answers (6)

superbeck
superbeck

Reputation: 501

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.

Upvotes: 0

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

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']

Upvotes: 0

haccks
haccks

Reputation: 106102

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.

Upvotes: 2

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24163

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

Upvotes: 0

hyades
hyades

Reputation: 3170

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

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599866

Don't do this. Readability counts.

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

Upvotes: 3

Related Questions