Challenge - 5 Problems
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested dictionary access
What is the output of this Python code?
Python
data = {'user': {'name': 'Alice', 'age': 30}, 'active': True}
print(data['user']['age'])Attempts:
2 left
๐ก Hint
Look inside the nested dictionary under 'user' key.
โ Incorrect
The code accesses the 'user' dictionary inside 'data' and then gets the value for 'age', which is 30.
๐ง Conceptual
intermediate1:30remaining
Dictionary keys uniqueness
Which statement about Python dictionary keys is true?
Attempts:
2 left
๐ก Hint
Think about what happens if you try to use a list as a key.
โ Incorrect
Dictionary keys must be unique and immutable types like strings, numbers, or tuples. Mutable types like lists cannot be keys.
๐ง Debug
advanced2:00remaining
Identify the error in dictionary update
What error does this code raise?
Python
d = {'a': 1, 'b': 2}
d.update(['c', 3])Attempts:
2 left
๐ก Hint
Check the argument type for update method.
โ Incorrect
The update method expects a mapping or iterable of key-value pairs. Passing a list of two elements causes a TypeError.
โ Predict Output
advanced1:30remaining
Result of dictionary comprehension with condition
What is the output of this code?
Python
result = {x: x**2 for x in range(5) if x % 2 == 0}
print(result)Attempts:
2 left
๐ก Hint
Only even numbers are included as keys.
โ Incorrect
The comprehension includes only keys where x is even, so keys 0, 2, and 4 with their squares as values.
๐ Application
expert2:30remaining
Count character frequency in string using dictionary
What is the value of 'freq' after running this code?
Python
text = 'banana' freq = {} for ch in text: freq[ch] = freq.get(ch, 0) + 1 print(freq)
Attempts:
2 left
๐ก Hint
Count how many times each letter appears in 'banana'.
โ Incorrect
The code counts each character's occurrences: 'b' once, 'a' three times, 'n' twice.