2026-05-05 11:56:15+08
Efficiency is key in Python development. List comprehensions offer a concise way to create lists, but they can become unreadable if not handled correctly. This prompt helps you generate clean, idiomatic code for data processing.
I have a Python list of dictionaries containing sales data: sales = [{'item': 'A', 'price': 10, 'qty': 2}, {'item': 'B', 'price': 20, 'qty': 1}]. Write a list comprehension that calculates the total revenue for each item and returns a new list of dictionaries with 'item' and 'total_revenue'.
Asking for a specific data structure (list of dictionaries) ensures the AI provides code that is ready to be integrated into a larger application or API response.
res = [{'item': s['item'], 'total_revenue': s['price'] * s['qty']} for s in sales]