0

I got a question about if-else block in Python, here is an example:

a = [1,2,3]
b = [i if i%2 else i+1 for i in a]
# following sentence is wrong
c = [i if i%2 for i in a else i+1]

But in some cases, if can be wrriten after for, just like this:

d = [i for i in a if a%2]

I wonder the reason about this, Thank you all!

This question has already been solved, Thank you again!!!

9
  • Your last example doesn't have for anywhere. Commented Jul 7, 2022 at 9:33
  • There's no for statement in the second part of the example. The syntax is pretty consistent to your b = statement Commented Jul 7, 2022 at 9:33
  • 1
    this is a list comprehension, search on the internet you easily found it. Commented Jul 7, 2022 at 9:34
  • You can use single expression in List comprehension and i if i%2 else i+1 work as ternary operator in python Commented Jul 7, 2022 at 9:38
  • There's literally thousands of tutorials in dozens of languages on that, why you did not do any research on your own? Commented Jul 7, 2022 at 9:45

2 Answers 2

2

There are multiple ways when you can actually use if else in Python. The very basic usage is of course the execution of block of code conditionally:

if <condition>:
    <body>
else:
    <body>

The if block will be executed when the condition is true, otherwise else block comes into execution.

Another use case is when you want to assign values conditionally:

x = y if <condition> else z

In above conditional assignment, the variable x is assigned value of y if the condition is true, other wise it is assigned the value of z. It is widely used in comprehension and return statements as well: [i if i%2==0 else i+1 for i in <iterable>]

Another use case is using if only, this is used in comprehension to filter out some values:

x = [i for i in <iterable> if <condition>]

The above expression will include only the items from iterable for which condition holds true.

Another use case is for else, it is quite unique to Python which allows using of else block after a loop:

for i in <iterable>:
   <loop body>
else:
    <else body>

The else part will be executed if loop exits normally without a break statement that means the else part is executed even if the code doesn't enter the loop body.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed description, I've learnt a lot in your answer!
1

This type of if-else expression is officially (in the Python docs) called a conditional expression, also known as a ternary conditional operator.

In your second and third examples, you combine this with a list comprehension - a for loop in list-brackets.

In your second example, you place the conditional expression (correctly) before the for loop of the list comprehension. This is a valid syntax. For each item that is looped over, the if-else expression is evaluated.

In your thrid example, you place the for loop between the if and the else. This is invalid syntax and to be honest, I don't know what you expect of the output.

In end of your question you state that in some cases the for can be put before the else (and after the if). In the example you give, there is no for.

2 Comments

Thank you! I used to be confused about ternary operator, and your answer is very beneficial to me
@wgt good to hear. If this is the “best” answer for you, please accept it by clicking the check-mark at the left of my answer.

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.