orders = [ {"order_id": 1, "customer_id": 101, "amount": 59.99, "status": "completed"}, {"order_id": 2, "customer_id": 102, "amount": 120.00, "status": "completed"}, {"order_id": 3, "customer_id": 101, "amount": 34.50, "status": "refunded"}, {"order_id": 4, "customer_id": 103, "amount": 89.00, "status": "completed"}, {"order_id": 5, "customer_id": 102, "amount": 45.00, "status": "completed"}, ] @timer def transform_orders(orders: list[dict]) -> list[dict]: # Compute lifetime value per customer ltv = {} for o in orders: if o["status"] == "completed": ltv[o["customer_id"]] = ltv.get(o["customer_id"], 0) + o["amount"] # Enrich each order with LTV and spend tier def spend_tier(value): if value >= 200: return "high" if value >= 100: return "mid" return "low" return [ {**o, "customer_ltv": ltv.get(o["customer_id"], 0), "spend_tier": spend_tier(ltv.get(o["customer_id"], 0))} for o in orders ] result = transform_orders(orders) for row in result: print(row)