Tracing update, a loop and a comprehension on stock levels

A greengrocer tracks how many crates of each fruit are in the storeroom. The Python code below merges a delivery record into the stock, tops up low items, removes some apples for a display, and then builds a dictionary of items that still need ordering.


Python
stock = {"apples": 12, "pears": 5, "plums": 8}
restock = {"pears": 10, "kiwis": 6}
stock.update(restock)
for fruit in stock:
    if stock[fruit] < 7:
        stock[fruit] += 4
stock["apples"] -= 3
low = {k: v for k, v in stock.items() if v < 10}


After the code runs, give each of the following as a whole number: the number of keys in stock, the value stored for "pears" in stock, the total of all values in stock, and the number of keys in low.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Dictionaries Questions: