0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Dictionary comprehension with condition
Start with iterable
Pick each item
Check condition?
NoSkip item
Yes
Add key:value to dict
More items?
YesPick next item
No
Return dict
This flow shows how dictionary comprehension picks items, checks a condition, and adds only those that pass into the new dictionary.
Execution Sample
Python
numbers = [1, 2, 3, 4, 5]
squares = {x: x*x for x in numbers if x % 2 == 0}
print(squares)
Creates a dictionary of squares for even numbers only from the list.
Execution Table
Stepx (current item)Condition (x % 2 == 0)ActionDictionary state
11FalseSkip{}
22TrueAdd 2:4{2: 4}
33FalseSkip{2: 4}
44TrueAdd 4:16{2: 4, 4: 16}
55FalseSkip{2: 4, 4: 16}
💡 All items processed; dictionary contains squares of even numbers only.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
x-12345-
squares{}{}{2: 4}{2: 4}{2: 4, 4: 16}{2: 4, 4: 16}{2: 4, 4: 16}
Key Moments - 2 Insights
Why does the dictionary not include squares of odd numbers?
Because the condition 'x % 2 == 0' filters out odd numbers, as shown in the execution_table rows where condition is False and action is Skip.
What happens if the condition is always False?
No items get added to the dictionary, so it remains empty. This is seen in the execution_table where all actions would be Skip.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the dictionary state after step 3?
A{2: 4}
B{2: 4, 4: 16}
C{}
D{1: 1, 2: 4}
💡 Hint
Check the 'Dictionary state' column at step 3 in the execution_table.
At which step does the condition 'x % 2 == 0' become True for the first time?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' column in the execution_table to find the first True.
If we change the condition to 'x > 3', what would be the dictionary state after step 5?
A{1: 1, 2: 4, 3: 9}
B{2: 4, 4: 16}
C{4: 16, 5: 25}
D{}
💡 Hint
Consider which numbers in the list are greater than 3 and what their squares are.
Concept Snapshot
Dictionary comprehension syntax:
{key_expr: value_expr for item in iterable if condition}
Creates a dict by looping, filtering with condition,
and adding key:value pairs only when condition is True.
Full Transcript
This visual execution shows how dictionary comprehension with a condition works in Python. We start with a list of numbers. For each number, we check if it meets the condition (being even). If yes, we add it as a key with its square as the value to the dictionary. If not, we skip it. The execution table tracks each step, showing the current number, condition result, action taken, and the dictionary's state. The variable tracker shows how 'x' and 'squares' change over time. Key moments clarify why odd numbers are skipped and what happens if no items meet the condition. The quiz tests understanding of dictionary state at different steps and effects of changing the condition.