Challenge - 5 Problems
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐ง Conceptual
intermediateWhy use dictionaries for fast data lookup?
Dictionaries in Python store data as key-value pairs. Why are they preferred for fast data lookup compared to lists?
Attempts:
2 left
๐ก Hint
Think about how you find a phone number in a phone book versus a list of names.
โ Incorrect
Dictionaries use a method called hashing to find values by keys quickly, unlike lists which require checking each item one by one.
โ Predict Output
intermediateOutput of dictionary key access
What is the output of this code?
Python
person = {'name': 'Alice', 'age': 30}
print(person['age'])Attempts:
2 left
๐ก Hint
The key 'age' is used to get the value from the dictionary.
โ Incorrect
Accessing person['age'] returns the value 30 stored under the key 'age'.
โ Predict Output
advancedWhat happens when accessing a missing key?
What error does this code raise?
Python
data = {'x': 10, 'y': 20}
print(data['z'])Attempts:
2 left
๐ก Hint
The key 'z' does not exist in the dictionary.
โ Incorrect
Trying to access a key that is not in the dictionary raises a KeyError.
๐ง Conceptual
advancedWhy dictionaries are useful for grouping related data?
Which reason best explains why dictionaries are used to group related data?
Attempts:
2 left
๐ก Hint
Think about how you label things in real life to find them easily.
โ Incorrect
Dictionaries let you label each piece of data with a key, so you can quickly find and understand what each value means.
โ Predict Output
expertNumber of items in a nested dictionary
What is the number of items in the dictionary after this code runs?
Python
inventory = {'fruits': {'apple': 10, 'banana': 5}, 'vegetables': {'carrot': 7}}
print(len(inventory))Attempts:
2 left
๐ก Hint
len() counts only the top-level keys in the dictionary.
โ Incorrect
The dictionary inventory has two top-level keys: 'fruits' and 'vegetables'. Nested dictionaries inside do not count as separate items at this level.
