Challenge - 5 Problems
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐ง Conceptual
intermediate2:00remaining
Why 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
intermediate1:30remaining
Output 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
advanced1:30remaining
What 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
advanced2:00remaining
Why 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
expert2:00remaining
Number 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.