This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Nested Data, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
How do you read the price from the first order?
orders = [{"drink": "latte", "price": 4.5}]Answer: orders[0]["price"]
Why: First select the record with [0], then read its price key.
What is printed?
data = {"shop": {"name": "Corner", "open": True}}
print(data["shop"]["name"])Answer: Corner
Why: The first key selects the inner dictionary; the second key reads its name.
Which expression collects every drink name?
orders = [{"drink": "latte"}, {"drink": "tea"}]Answer: [o["drink"] for o in orders]
Why: A list comprehension reads the drink key from each record.
What does sum(o["price"] for o in orders) calculate?
Answer: The total of all price fields
Why: The generator expression yields each price, and sum adds them.
Complete the key that prints latte.
orders = [{"drink": "latte", "price": 4.5}]
print(orders[0]["____"])Answer: drink
Why: The dictionary stores the drink name under the drink key.
Complete the key that prints Corner.
data = {"shop": {"name": "Corner"}}
print(data["shop"]["____"])Answer: name
Why: After selecting data["shop"], read its name field.
Complete the field name being updated.
orders = [{"price": 3.0}]
orders[0]["____"] = 4.0
print(orders[0]["price"])Answer: price
Why: Assignment to orders[0]["price"] changes the price field on the first record.
Complete the update so total accumulates each price.
orders = [{"price": 2.0}, {"price": 3.0}]
total = 0
for order in orders:
total ____ order["price"]
print(total)Answer: +=
Why: total += order["price"] adds each record's price to the running total.
What is the output?
data = {"a": {"b": 2}}
print(data["a"]["b"])Answer: 2
Why: The outer key a returns the inner dictionary, whose b value is 2.
What does this code print?
orders = [{"price": 3}, {"price": 4}]
print(sum(o["price"] for o in orders))Answer: 7
Why: The prices 3 and 4 add up to 7.
What does the program output?
data = {"items": [1, 2, 3]}
print(len(data["items"]))Answer: 3
Why: The inner value is a list with three items.
What appears on the screen?
orders = [{"drink": "tea"}, {"drink": "latte"}]
print(orders[1]["drink"])Answer: latte
Why: Index 1 selects the second record, whose drink is latte.
Try the questions first
Open the practice page for Python Nested Data and answer the same 12 questions with instant feedback.