Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is dictionary comprehension in Python?
Dictionary comprehension is a concise way to create dictionaries by writing an expression inside curly braces, using a loop and optionally a condition.
Click to reveal answer
beginner
How do you add a condition in a dictionary comprehension?
You add an if statement after the for loop inside the comprehension to filter which items are included.
Click to reveal answer
beginner
Example: What does this code do?
{x: x*x for x in range(5) if x % 2 == 0}
It creates a dictionary with keys as even numbers from 0 to 4, and values as their squares. Result: {0: 0, 2: 4, 4: 16}
Click to reveal answer
intermediate
Can dictionary comprehension with condition replace loops?
Yes, it can replace loops for creating dictionaries when you want to filter items, making code shorter and easier to read.
Click to reveal answer
beginner
What happens if the condition in dictionary comprehension is always false?
The resulting dictionary will be empty because no items meet the condition to be included.
Click to reveal answer
Which part of this dictionary comprehension is the condition?
{k: v for k, v in data.items() if v > 10}
Aif v > 10
Bfor k, v in data.items()
Ck: v
Ddata.items()
✗ Incorrect
The condition filters items where the value is greater than 10.
What will this code output?
{i: i+1 for i in range(3) if i == 5}
A{}
B{5: 6}
C{0: 1, 1: 2, 2: 3}
DError
✗ Incorrect
No number in range(3) equals 5, so the dictionary is empty.
How do you write a dictionary comprehension that includes only keys that start with 'a' from a dictionary d?
A{k: v for v in d.values() if v.startswith('a')}
B{k: v for k, v in d.items() if v.startswith('a')}
C{k: v for k, v in d.items() if k.startswith('a')}
D{k: v for k in d if k == 'a'}
✗ Incorrect
The condition filters keys starting with 'a'.
Which of these is NOT a valid use of condition in dictionary comprehension?
AFiltering items to include
BChanging keys based on condition
CAdding multiple conditions with 'and' or 'or'
DUsing condition outside the comprehension braces
✗ Incorrect
Conditions must be inside the comprehension braces, after the for loop.
What does this dictionary comprehension do?
{x: x**2 for x in range(6) if x % 2 != 0}
ACreates squares of even numbers from 0 to 5
BCreates squares of odd numbers from 0 to 5
CCreates squares of all numbers from 0 to 5
DCreates squares of numbers divisible by 2
✗ Incorrect
The condition x % 2 != 0 selects odd numbers.
Explain how to create a dictionary comprehension with a condition to include only items where the value is greater than 10.
Think about filtering items while creating the dictionary.
You got /4 concepts.
Describe what happens if the condition in a dictionary comprehension never matches any item.
What does an empty filter produce?
You got /3 concepts.
Practice
(1/5)
1.
What does the following dictionary comprehension do? {k: v for k, v in {'a': 1, 'b': 2, 'c': 3}.items() if v > 1}
easy
A. {'a': 1, 'b': 2, 'c': 3} - keeps all items
B. {'a': 1} - keeps items with values equal to 1
C. {'b': 2, 'c': 3} - keeps items with values greater than 1
D. {} - removes all items
Solution
Step 1: Understand the dictionary comprehension structure
The comprehension loops over each key-value pair in the original dictionary.
Step 2: Apply the condition if v > 1
Only pairs where the value is greater than 1 are included in the new dictionary.
Final Answer:
{'b': 2, 'c': 3} - keeps items with values greater than 1 -> Option C
Quick Check:
Filter values > 1 = {'b': 2, 'c': 3} [OK]
Hint: Look for the condition after the for loop to filter items [OK]
Common Mistakes:
Ignoring the condition and including all items
Confusing keys and values in the condition
Using wrong comparison operator
2.
Which of the following is the correct syntax for a dictionary comprehension with a condition?
?
easy
A. {k: v for k, v in d.items() if v % 2 == 0}
B. {k: v if v % 2 == 0 for k, v in d.items()}
C. {k: v for k, v in d.items() where v % 2 == 0}
D. {k: v for k, v in d.items() when v % 2 == 0}
Solution
Step 1: Recall dictionary comprehension syntax
The correct syntax is {key: value for key, value in iterable if condition}.
Step 2: Identify the correct option
{k: v for k, v in d.items() if v % 2 == 0} matches the correct syntax with if after the loop. Others use invalid keywords or wrong order.
Final Answer:
{k: v for k, v in d.items() if v % 2 == 0} -> Option A
Quick Check:
Correct syntax uses 'if' after for loop [OK]
Hint: Remember: condition goes after the for clause in comprehension [OK]
Common Mistakes:
Placing 'if' before the for loop
Using 'where' or 'when' instead of 'if'
Incorrect order of clauses
3.
What is the output of this code?
nums = {'x': 10, 'y': 5, 'z': 0}
filtered = {k: v for k, v in nums.items() if v}
print(filtered)
medium
A. {'x': 10, 'y': 5}
B. {}
C. {'z': 0}
D. {'x': 10, 'y': 5, 'z': 0}
Solution
Step 1: Understand the condition if v
This condition filters out values that are 'falsy' in Python, like 0, None, or empty.
Step 2: Apply the condition to each item
Values 10 and 5 are truthy, so included; 0 is falsy, so excluded.
Final Answer:
{'x': 10, 'y': 5} -> Option A
Quick Check:
Falsy values excluded = {'x': 10, 'y': 5} [OK]
Hint: Falsy values like 0 are skipped when condition is just 'if v' [OK]
Common Mistakes:
Including zero as truthy
Confusing keys and values
Expecting all items to be included
4.
Find the error in this dictionary comprehension:
data = {'a': 1, 'b': 2, 'c': 3}
result = {k: v for k, v in data.items() if v > 1 else 0}
print(result)
medium
A. No error, outputs {'b': 2, 'c': 3}
B. SyntaxError due to incorrect use of else in comprehension
C. Outputs {'a': 0, 'b': 2, 'c': 3}
D. KeyError because of else clause
Solution
Step 1: Check the use of else in comprehension condition
Dictionary comprehensions cannot have an else clause directly after the if condition.
Step 2: Identify correct syntax
To use else, it must be inside a value expression with a ternary operator, not after if.
Final Answer:
SyntaxError due to incorrect use of else in comprehension -> Option B
Quick Check:
else must be inside value expression, not after if [OK]
Hint: Use ternary inside value, not else after if in comprehension [OK]
Common Mistakes:
Placing else after if in comprehension
Confusing ternary operator syntax
Expecting else to filter keys
5.
You have a dictionary of student scores: scores = {'Alice': 85, 'Bob': 42, 'Charlie': 73, 'David': 58} Use dictionary comprehension with a condition to create a new dictionary passed containing only students who scored 60 or more, but store their scores as 'Pass' instead of the number. Which code correctly does this?
hard
A. {k: 'Pass' for k, v in scores.items() if v > 60}
B. {k: 'Pass' if v >= 60 else 'Fail' for k, v in scores.items()}
C. {k: v for k, v in scores.items() if v >= 60}
D. {k: 'Pass' for k, v in scores.items() if v >= 60}
Solution
Step 1: Understand the requirement
We want only students with scores 60 or more, and their values replaced by 'Pass'.
Step 2: Check each option
{k: 'Pass' for k, v in scores.items() if v >= 60} filters with if v >= 60 and sets value to 'Pass'. {k: 'Pass' if v >= 60 else 'Fail' for k, v in scores.items()} includes all students with ternary but no filtering. {k: v for k, v in scores.items() if v >= 60} keeps original scores. {k: 'Pass' for k, v in scores.items() if v > 60} excludes 60 exactly.
Final Answer:
{k: 'Pass' for k, v in scores.items() if v >= 60} -> Option D
Quick Check:
Filter and replace value with 'Pass' = {k: 'Pass' for k, v in scores.items() if v >= 60} [OK]
Hint: Filter with if, set value directly to 'Pass' in comprehension [OK]