pythonadvanced15 minutes

Refactor Complex Data Aggregation for Readability and Performance in Python

You are given a Python function that processes a nested list of sales data to compute total sales per product category, but the code is messy, hard to read, and inefficient. Refactor the function to improve code quality without changing its behavior.

Challenge prompt

The provided function takes a list of sales records where each record is a dictionary containing 'category', 'product', and 'amount'. It calculates the total sales amount per category. The current implementation uses nested loops, redundant variables, and repeated conditional checks, resulting in unmaintainable and suboptimal code. Refactor this function to enhance readability, reduce complexity, and improve performance while preserving the exact output.

Guidance

  • Preserve the output structure: a dictionary mapping each category to its total sales amount.
  • Use built-in Python features like collections.defaultdict or itertools to simplify aggregation.
  • Avoid repeated lookups and redundant variables to reduce computational overhead.

Hints

  • Consider using defaultdict from the collections module to remove the need for manual key checks.
  • Replace nested loops with comprehensions or functions that process data in a single pass.
  • Focus on writing clear, descriptive variable names and modular code.

Starter code

def calculate_total_sales(sales_data):
    result = {}
    for record in sales_data:
        category = record['category']
        amount = record['amount']
        if category not in result:
            result[category] = 0
        current_total = result[category]
        for other_record in sales_data:
            if other_record['category'] == category:
                current_total += other_record['amount']
        result[category] = current_total
    return result

Expected output

{'electronics': 4500, 'furniture': 2700, 'clothing': 1200}

Core concepts

code refactoringdictionary aggregationcollections.defaultdictperformance optimization

Challenge a Friend

Send this duel to someone else and see if they can solve it.