Answer key · 12 questions

Python Nested Data Exercises With Answers

All 12 questions for Python Nested Data in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

How do you read the price from the first order?

orders = [{"drink": "latte", "price": 4.5}]
  1. orders[0].price
  2. orders[0]["price"]correct
  3. orders.price[0]
  4. orders["price"][0]

Answer: orders[0]["price"]

Why: First select the record with [0], then read its price key.

02 · Multiple choice

What is printed?

data = {"shop": {"name": "Corner", "open": True}}
print(data["shop"]["name"])
  1. True
  2. shop
  3. Cornercorrect
  4. name

Answer: Corner

Why: The first key selects the inner dictionary; the second key reads its name.

03 · Multiple choice

Which expression collects every drink name?

orders = [{"drink": "latte"}, {"drink": "tea"}]
  1. orders[0]["drink"]
  2. [o["drink"] for o in orders]correct
  3. orders["drink"]
  4. for o in orders: o.drink

Answer: [o["drink"] for o in orders]

Why: A list comprehension reads the drink key from each record.

04 · Multiple choice

What does sum(o["price"] for o in orders) calculate?

  1. The largest price
  2. A list of prices
  3. The number of orders
  4. The total of all price fieldscorrect

Answer: The total of all price fields

Why: The generator expression yields each price, and sum adds them.

05 · Fill in the blank

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.

06 · Fill in the blank

Complete the key that prints Corner.

data = {"shop": {"name": "Corner"}}
print(data["shop"]["____"])

Answer: name

Why: After selecting data["shop"], read its name field.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson