Codington
0 of 6 problems solved • 0%

Exercise prompt

Index the Catalog

A dictionary comprehension gives a key and a value for each included item. If several pairs produce the same key, the later value replaces the earlier one.

Example

Build a lookup table from three bus departures.

stops = [("A", "08:00"), ("B", "08:15"), ("A", "09:00")] next_time = {stop: time for stop, time in stops} print(next_time) # {'A': '09:00', 'B': '08:15'}

The later departure overwrites the earlier time for A. Add a filter when only certain records should be allowed to change a stored value.

Your task

Build a dictionary mapping each in-stock product name to its positive quantity. The input is a list of (name, quantity) pairs. Ignore zero and negative quantities before building the index. For repeated names, keep the last positive quantity.

catalog_index([("pen", 2), ("pad", 0), ("pen", 4)]) # {"pen": 4}