Challenge - 5 Problems
Dictionary Creation 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 in 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
Output of dictionary from zip function
What does this code print?
Python
keys = ['a', 'b', 'c'] values = [1, 2, 3] result = dict(zip(keys, values)) print(result)
Attempts:
2 left
๐ก Hint
zip pairs elements from two lists into tuples.
โ Incorrect
dict(zip(keys, values)) creates a dictionary pairing each key with its corresponding value.
โ 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(3)} for x in range(2)}
print(result)Attempts:
2 left
๐ก Hint
The inner dictionary multiplies x by y for y in 0 to 2.
โ Incorrect
For x=0, all products are 0. For x=1, products are 0,1,2. So nested dicts reflect these values.
โ Predict Output
advanced2:30remaining
Output of dictionary creation with duplicate keys
What is the output of this code?
Python
result = {x % 3: x for x in range(6)}
print(result)Attempts:
2 left
๐ก Hint
Keys repeat because of modulo operation; later values overwrite earlier ones.
โ Incorrect
Keys are 0,1,2 repeated twice. The last value for each key is kept in the dictionary.
๐ง Conceptual
expert2:00remaining
Error raised by invalid dictionary creation syntax
What error does this code raise?
Python
result = {x: x*2 if x > 2 for x in range(5)}Attempts:
2 left
๐ก Hint
Check the placement of the if condition in the dictionary comprehension.
โ Incorrect
The syntax is invalid because the if condition is inside the value expression without else, causing SyntaxError.