Challenge - 5 Problems
Dictionary Comprehension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of dictionary comprehension with condition
What is the output of this Python code?
Python
result = {x: x**2 for x in range(5) if x % 2 == 0}
print(result)Attempts:
2 left
๐ก Hint
Look at the condition after the for loop inside the comprehension.
โ Incorrect
The comprehension includes only even numbers (x % 2 == 0). So keys are 0, 2, 4 with their squares as values.
โ Predict Output
intermediate2:00remaining
Dictionary comprehension with ternary expression
What does this code print?
Python
result = {x: ('even' if x % 2 == 0 else 'odd') for x in range(4)}
print(result)Attempts:
2 left
๐ก Hint
Check how the ternary operator assigns values based on even or odd keys.
โ Incorrect
The ternary expression assigns 'even' if x is divisible by 2, else 'odd'.
โ Predict Output
advanced2:30remaining
Output of nested dictionary comprehension
What is the output of this code?
Python
result = {x: {y: x*y for y in range(2)} for x in range(3)}
print(result)Attempts:
2 left
๐ก Hint
Look carefully at the inner dictionary comprehension and how multiplication works.
โ Incorrect
For each x, the inner dictionary maps y to x*y for y in 0 and 1.
โ Predict Output
advanced2:00remaining
Result of dictionary comprehension with string keys
What will this code output?
Python
keys = ['a', 'b', 'c'] result = {k: ord(k) for k in keys} print(result)
Attempts:
2 left
๐ก Hint
The ord() function returns the Unicode code point of a character.
โ Incorrect
Each key maps to its Unicode number using ord().
๐ง Conceptual
expert2:00remaining
Number of items in dictionary comprehension result
How many items are in the dictionary created by this comprehension?
{n: n//2 for n in range(10) if n % 3 != 0}
Attempts:
2 left
๐ก Hint
Count numbers from 0 to 9 excluding multiples of 3.
โ Incorrect
Numbers 0,3,6,9 are multiples of 3 and excluded. Remaining 6 numbers create 6 items.