File size: 906 Bytes
360b354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)