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 key-value pair format, often with a loop.
Click to reveal answer
beginner
Write a simple dictionary comprehension to create a dictionary with numbers 1 to 3 as keys and their squares as values.
Example: {x: x**2 for x in range(1, 4)} creates {1: 1, 2: 4, 3: 9}.
Click to reveal answer
beginner
What are the parts of a dictionary comprehension?
It has three parts: the key expression, the value expression, and the for loop that generates items.
Click to reveal answer
intermediate
Can dictionary comprehension include conditions? Give a simple example.
Yes, you can add an if condition to filter items. Example: {x: x**2 for x in range(5) if x % 2 == 0} creates {0: 0, 2: 4, 4: 16}.
Click to reveal answer
beginner
Why use dictionary comprehension instead of a for loop?
Dictionary comprehension is shorter, easier to read, and often faster than building a dictionary with a for loop.
Click to reveal answer
What does this dictionary comprehension do? {x: x+1 for x in range(3)}
ACreates {0: 1, 1: 2, 2: 3}
BCreates {1: 0, 2: 1, 3: 2}
CCreates {0: 0, 1: 1, 2: 2}
DCreates {1: 1, 2: 2, 3: 3}
✗ Incorrect
For each x in 0,1,2, the key is x and the value is x+1, so the dictionary is {0:1, 1:2, 2:3}.
Which syntax is correct for dictionary comprehension?
A[key: value for item in iterable]
B(key: value for item in iterable)
C{key: value for item in iterable}
D{key, value for item in iterable}
✗ Incorrect
Dictionary comprehension uses curly braces with key:value pairs and a for loop.
How do you add a condition to a dictionary comprehension?
AYou cannot add conditions in dictionary comprehension
BAdd if condition before the for loop
CAdd if condition inside the key expression
DAdd if condition after the for loop
✗ Incorrect
You add an if condition after the for loop to filter items.
What will {x: x*2 for x in range(3) if x != 1} produce?
A{1: 2, 2: 4}
B{0: 0, 2: 4}
C{0: 0, 1: 2, 2: 4}
D{2: 4}
✗ Incorrect
It skips x=1 because of the condition, so keys 0 and 2 with values 0 and 4.
Why might dictionary comprehension be preferred over a for loop?
AIt is more concise and readable
BIt uses more lines of code
CIt is slower
DIt cannot include conditions
✗ Incorrect
Dictionary comprehension is shorter and easier to read than a for loop.
Explain how to create a dictionary using dictionary comprehension with an example.
Think about how to turn a list of numbers into a dictionary of squares.
You got /4 concepts.
Describe how to add a condition to dictionary comprehension and why it might be useful.
Consider only including even numbers as keys.
You got /4 concepts.
Practice
(1/5)
1. What does the following dictionary comprehension do? {x: x*x for x in range(3)}
easy
A. Creates a list of squares from 0 to 2
B. Creates a set of squares from 0 to 2
C. Creates a dictionary with numbers 0 to 2 as keys and their squares as values
D. Creates a dictionary with keys as squares and values as numbers
Solution
Step 1: Understand the loop and key-value pairs
The comprehension loops over numbers 0, 1, 2 and uses each number as the key.
Step 2: Calculate the value for each key
Each value is the square of the key (x*x), so values are 0, 1, 4 respectively.
Final Answer:
Creates a dictionary with numbers 0 to 2 as keys and their squares as values -> Option C
Hint: Remember dictionary comprehension uses curly braces and colon [OK]
Common Mistakes:
Using square brackets instead of curly braces
Using arrow (=>) instead of colon for key-value
Using parentheses which create generator expressions
3. What is the output of this code?
nums = [1, 2, 3]
squares = {n: n**2 for n in nums if n % 2 != 0}
print(squares)
medium
A. {1: 1, 3: 9}
B. {2: 4}
C. {1: 1, 2: 4, 3: 9}
D. {}
Solution
Step 1: Understand the filtering condition
The comprehension includes only numbers where n % 2 != 0, meaning odd numbers only (1 and 3).
Step 2: Calculate squares for filtered numbers
Squares are 1*1=1 and 3*3=9, so dictionary is {1:1, 3:9}.
Final Answer:
{1: 1, 3: 9} -> Option A
Quick Check:
Filter odd numbers and square them = {1:1, 3:9} [OK]
Hint: Check the if condition filters keys before squaring [OK]
Common Mistakes:
Including even numbers by mistake
Confusing list comprehension output with dictionary
Ignoring the if condition in comprehension
4. Find the error in this dictionary comprehension:
result = {x, x*2 for x in range(3)}
medium
A. Missing colon between key and value
B. Using parentheses instead of curly braces
C. Range function is incorrect
D. No error, code is correct
Solution
Step 1: Check syntax for key-value pairs
Dictionary comprehension requires a colon ':' between key and value, but here a comma ',' is used.
Step 2: Confirm other parts are correct
Curly braces and range(3) are correct, so the error is the missing colon.
Final Answer:
Missing colon between key and value -> Option A
Quick Check:
Key:value pairs need colon, not comma [OK]
Hint: Use colon ':' to separate key and value in dict comprehension [OK]
Common Mistakes:
Using comma instead of colon between key and value
Confusing set comprehension with dictionary comprehension
Using wrong brackets for comprehension
5. Given a list of words, create a dictionary comprehension that maps each word to its length but only include words longer than 3 letters. Which code correctly does this?
words = ['cat', 'house', 'dog', 'elephant']
hard
A. {word: len(word) for word in words where len(word) > 3}
B. {word: len(word) for word in words if word > 3}
C. {word: len(word) if len(word) > 3 for word in words}
D. {word: len(word) for word in words if len(word) > 3}
Solution
Step 1: Understand filtering condition syntax
The if condition must come after the for loop to filter words by length correctly.
Step 2: Check each option's syntax
{word: len(word) for word in words if len(word) > 3} correctly places 'if len(word) > 3' after the for loop. Others have syntax errors or wrong condition placement.
Final Answer:
{word: len(word) for word in words if len(word) > 3} -> Option D
Quick Check:
Filter after for loop with if condition [OK]
Hint: Put if condition after for loop in dictionary comprehension [OK]