0
0
Pythonprogramming~10 mins

List comprehension with condition in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - List comprehension with condition
Start with a list
For each item in list
Check condition?
NoSkip item
Yes
Apply expression to item
Add result to new list
Repeat for all items
Return new filtered list
This flow shows how list comprehension goes through each item, checks a condition, and includes only those items that meet the condition in the new list.
Execution Sample
Python
numbers = [1, 2, 3, 4, 5]
even_squares = [x*x for x in numbers if x % 2 == 0]
print(even_squares)
This code creates a new list of squares of even numbers from the original list.
Execution Table
StepCurrent item (x)Condition (x % 2 == 0)ActionNew list state
11FalseSkip 1[]
22TrueAdd 4 (2*2)[4]
33FalseSkip 3[4]
44TrueAdd 16 (4*4)[4, 16]
55FalseSkip 5[4, 16]
End--Finished iteration[4, 16]
💡 All items processed; only even numbers squared and added to new list.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
x-12345-
even_squares[][][4][4][4, 16][4, 16][4, 16]
Key Moments - 3 Insights
Why are some items skipped and not added to the new list?
Items are skipped when the condition (x % 2 == 0) is False, as shown in steps 1, 3, and 5 in the execution table where the action is 'Skip'.
What does the expression 'x*x' do in the list comprehension?
It calculates the square of the current item x, which is added to the new list only if the condition is True, as seen in steps 2 and 4.
Is the original list changed by the list comprehension?
No, the original list 'numbers' remains unchanged; the comprehension creates a new list 'even_squares' as shown in the variable tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the new list after processing the item 4?
A[4]
B[16]
C[4, 16]
D[]
💡 Hint
Check the 'New list state' column at step 4 in the execution table.
At which step does the condition become False for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' column in the execution table; step 1 shows False first.
If we change the condition to 'x > 3', what will be the new list after step 5?
A[16, 25]
B[4, 16]
C[9, 16, 25]
D[1, 2, 3, 4, 5]
💡 Hint
Check which numbers are greater than 3 and their squares.
Concept Snapshot
List comprehension with condition syntax:
  [expression for item in iterable if condition]
Creates a new list by including only items that satisfy the condition.
Expression is applied only to items passing the condition.
Original list remains unchanged.
Useful for filtering and transforming lists in one line.
Full Transcript
This visual trace shows how list comprehension with a condition works in Python. We start with a list of numbers. For each number, we check if it is even (condition x % 2 == 0). If it is, we square it and add it to a new list. If not, we skip it. The execution table tracks each step, showing which items are processed, which are skipped, and how the new list grows. The variable tracker shows the values of the current item and the new list after each step. Key moments clarify why some items are skipped, what the expression does, and that the original list is not changed. The quiz questions help reinforce understanding by asking about the list state at specific steps and the effect of changing the condition.