Step: Nested data: sum over a list of dicts

Nested data: sum over a list of dicts

sum(expr for item in iterable) combines a generator expression (like a list comprehension, but without the [...]) with the built-in sum() to total up a computed value across items.

Why it matters: aggregating over a list of dicts (rows of data) is one of the most common real-world Python tasks.

Example: sum(x * 2 for x in [1, 2, 3]) is 12.

Your turn: compute the total inventory value (qty * price for each item, summed) for inventory.

Pitfall: mixing an int qty with a float price promotes the whole sum to float — that's why the answer is 31.0, not 31.

Setup:
inventory = [
    {"item": "Widget", "qty": 4, "price": 2.5},
    {"item": "Gadget", "qty": 1, "price": 9.0},
    {"item": "Gizmo", "qty": 3, "price": 4.0},
]
Your code:
Expected output:
31.0
Step 16 of 18