← All Python interview questions
Total campaign revenue from prices and quantities
EasyLists & LoopsFlipkartMeesho
Marketing sends you two parallel lists: the unit price of each item sold in a campaign and the quantity sold. You need one revenue number for the campaign report.
Write a function total_revenue(prices, quantities) that multiplies each price by the quantity at the same position, sums the results, and returns the total as a float rounded to 2 decimals.
This fixture runs before your code on every Run and Submit, so the data is always clean.
prices = [199.0, 49.5, 1299.0, 25.25]
quantities = [3, 10, 1, 4]- Step 1: `zip(prices, quantities)` walks both lists together and yields `(price, quantity)` pairs — no index bookkeeping needed.
- Step 2: The generator expression `p * q for p, q in zip(...)` produces one line-item revenue per pair, and `sum()` folds them into a single number.
- Step 3: `float(total)` guarantees a float even when every input is an integer (an empty campaign would otherwise return the integer `0`).
- Step 4: `round(..., 2)` matches the currency precision the finance team expects.
def total_revenue(prices, quantities):
total = sum(p * q for p, q in zip(prices, quantities))
return round(float(total), 2)Learn the concept first
Stuck on Lists & Loops? These free Python lessons teach it step by step.
More Lists & Loops questions
- This is the only question on this topic for now.
Asked at Flipkart
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program