Spaces:
Runtime error
Runtime error
from collections import defaultdict | |
def count_items_ordered(data): | |
""" | |
Count the number of times each item is ordered. | |
Args: | |
data (dict): A dictionary containing order data with details about items. | |
Returns: | |
dict: A dictionary with item titles as keys and their order counts as values. | |
""" | |
item_count = defaultdict(int) | |
for order in data.get("data", []): | |
for item in order.get("data", [])[0].get("order", {}).get("items", []): | |
item_title = item.get("title", "") | |
quantity = item.get("quantity", 0) | |
if item_title: | |
try: | |
quantity = int(quantity) | |
item_count[item_title] += quantity | |
except ValueError: | |
print(f"Skipping item {item_title} with invalid quantity: {quantity}") | |
pass | |
return dict(item_count) | |