Refactor and Optimize Complex Data Processing Function
Given a large-function script that processes a nested data structure with redundant loops and poor readability, refactor the code to improve maintainability, reduce complexity, and optimize performance without changing its output.
Challenge prompt
You are provided a Python function `process_data(data)` that takes a list of dictionaries with nested data. The function performs multiple nested loops, redundant computations, and has low readability due to variable naming and structure. Your task is to refactor this function to: - Improve readability and maintainability by using meaningful variable names and modularizing repeated logic into helper functions. - Optimize performance by minimizing redundant iterations and using efficient data access techniques. - Keep the behavior of the function exactly the same, ensuring output correctness. Submit the refactored version of the function only.
Guidance
- • Break down the monolithic function into smaller helper functions with single responsibilities.
- • Use list comprehensions or generator expressions where appropriate to simplify loops.
- • Avoid redundant loops by combining data processing steps when possible.
- • Rename variables to clearly indicate their purpose and data content.
Hints
- • Consider using dictionary lookups to replace repeated searches through the list.
- • Extract reusable blocks of code into separate functions for clarity.
- • Use pythonic constructs like enumerate and zip to simplify nested loops.
Starter code
def process_data(data):
result = []
for i in range(len(data)):
temp = []
for j in range(len(data[i]['items'])):
item = data[i]['items'][j]
if item['value'] > 10:
acc = 0
for k in range(len(item['subitems'])):
subitem = item['subitems'][k]
acc += subitem['metric']
temp.append({'name': item['name'], 'total_metric': acc})
result.append({'id': data[i]['id'], 'processed': temp})
return resultExpected output
[{'id': 1, 'processed': [{'name': 'itemA', 'total_metric': 25}]}, {'id': 2, 'processed': []}]
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.