pythonlistdictionarylist-manipulation

Group list of dictionaries based on key


I have a array that looks like this

comp_data = [
  {
    "product_id": 432263,
    "price_zone_id": 1,
    "oreilly_price": 0,
    "amazon_price": 0,
    "az_price": 0,
    "rockauto_price": 0,
    "napa_price": 0,
    "oreilly_index": 0,
    "amazon_index": 0,
    "az_index": 0,
    "rockauto_index": 0,
    "napa_index": 0,
    "weighted_comp_price": 0,
    "weighted_comp_index": 0,
    "week": None
  }
]

Skipping the fields product_id and price_zone_id, I want to create a list of dictionaries by mapping the names of each key. For Example

You can see amazon_price, amazon_index, ultimately I want a list that looks like this

[
    {
        'amazon_price': 0,
        'amazon_index': 0,
        'name': 'Amazon' --> This would be simply adding .title() to the name. 
    },
    {
        'az_price': 0,
        'az_index': 0,
        'name': 'Az' --> This would be simply adding .title() to the name. 
    },
    {
        'orielly_price': 0,
        'orielly_index': 0,
        'name': 'ORielly' --> This would be simply adding .title() to the name. 
    }
]

My current code looks like this and is generating wrong output

stores_data = {}
for data in comp_data:
    dict_keys = data.keys()
    for keys in dict_keys:
        if keys != 'product_id' and keys != 'price_zone_id':
            store_name = keys.split('_')[0]
            value_type = keys.split('_')[-1]
            stores[store_name][value_type] =  {}

Stores are eassentially the string infront of _index or _price. ex amazon_index, store would be amazon

For this key "weighted_comp_price", the store would be Weighted Comp


Solution

  • For each dict

    1. Find the _price keys
    2. Verify you have the corresponding _index key
    3. Save it
    stores_data = []
    for data in comp_data:
        for price_key in (k for k in data if k.endswith("_price")):
            name = price_key.rsplit("_", maxsplit=1)[0]
            index_key = f'{name}_index'
            if index_key in data:
                stores_data.append({price_key: data[price_key], index_key: data[index_key], 'name': name.title()})
    

    Giving

    [{'oreilly_price': 0, 'oreilly_index': 0, 'name': 'Oreilly'}, 
     {'az_price': 0, 'az_index': 0, 'name': 'Az'}, 
     {'napa_price': 0, 'napa_index': 0, 'name': 'Napa'}, 
     {'amazon_price': 0, 'amazon_index': 0, 'name': 'Amazon'}, 
     {'rockauto_price': 0, 'rockauto_index': 0, 'name': 'Rockauto'}, 
     {'weighted_comp_price': 0, 'weighted_comp_index': 0, 'name': 'Weighted_Comp'}]