0
0
Pythonprogramming~10 mins

List comprehension with if–else in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - List comprehension with if–else
Start with input list
For each item in list
Check condition?
Apply if-part
Add result to new list
Repeat for all items
Return new list
We go through each item in the list, check a condition, then choose a value based on if the condition is true or false, building a new list.
Execution Sample
Python
numbers = [1, 2, 3, 4]
result = [x*2 if x % 2 == 0 else x+1 for x in numbers]
print(result)
This code doubles even numbers and adds 1 to odd numbers in the list.
Execution Table
Stepx (current item)Condition (x % 2 == 0)ActionResult added to listList so far
11Falsex+12[2]
22Truex*24[2, 4]
33Falsex+14[2, 4, 4]
44Truex*28[2, 4, 4, 8]
💡 All items processed, list comprehension ends with final list.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
x-1234-
result[][2][2, 4][2, 4, 4][2, 4, 4, 8][2, 4, 4, 8]
Key Moments - 3 Insights
Why do we write the if–else before the for loop in the comprehension?
In list comprehension with if–else, the if–else expression comes before the for loop because it decides the value to add for each item. The execution_table rows show the condition and action happen for each x before moving to the next.
What happens if the condition is true or false?
If the condition is true, the 'if' part runs (like x*2). If false, the 'else' part runs (like x+1). The execution_table shows this clearly in the 'Action' and 'Result added' columns.
Is this the same as filtering items?
No. This if–else chooses what value to add for every item. Filtering would skip items. Here, every item produces a result, as seen in every step of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 3?
A[2, 4, 3]
B[2, 4, 4]
C[1, 2, 3]
D[2, 4, 6]
💡 Hint
Check the 'List so far' column in row 3 of the execution_table.
At which step does the condition 'x % 2 == 0' become True for the first time?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Condition' column in the execution_table rows.
If we change the condition to 'x > 2', what would be the result added at step 2?
A1
B4
C3
D5
💡 Hint
Step 2 has x=2; check if 2 > 2 is True or False and what action applies.
Concept Snapshot
List comprehension with if–else syntax:
[result_if_true if condition else result_if_false for item in iterable]

- Checks condition for each item
- Chooses one of two results
- Builds new list with all results

Useful for transforming lists with choices.
Full Transcript
This lesson shows how list comprehension with if–else works in Python. We start with a list of numbers. For each number, we check if it is even. If yes, we double it. If no, we add one. We add the result to a new list. This repeats for all items. The final list contains transformed values based on the condition. The execution table tracks each step, showing the current item, condition result, action taken, and the list building up. Key points include the placement of if–else before the for loop, how both true and false cases produce results, and that this is not filtering but transforming every item. The quiz checks understanding of the list state at steps, condition truth, and effect of changing the condition.