Nested Order Total Traversal

Medium ⏱ 12 min 54% acceptance ★★★★☆ 4.3
Write a function order_total(order) where order is a nested dict shaped like {"id": 1, "items": [{"name": "Pen", "price": 10, "qty": 3}, {"name": "Bag", "price": 500, "qty": 1}]}. Return the total cost: the sum of price * qty across every dict in order["items"].

Examples

Example 1
Input
order_total({'id': 1, 'items': [{'name': 'Pen', 'price': 10, 'qty': 3}, {'name': 'Bag', 'price': 500, 'qty': 1}]})
Output
530
Explanation

10*3 + 500*1 = 30 + 500 = 530.

Constraints

  • `order["items"]` is a list of dicts, each with price and qty keys.

Topics

Dictionariesnested traversal

Companies

AirbnbAtlassian

Hints

Hint 1

`sum(item["price"] * item["qty"] for item in order["items"])`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.