python dictionary how can create (structured) unique dictionary list if the key contains list of values of other keys

I have below unstructured dictionary list which contains values of other keys in a list .

I am not sure if the question i ask is strange. this is the actual dictionary payload that we receive from source which not aligned with respective entry

[
  {
    "dsply_nm": [
      "test test",
      "test test",
      "",
      ""
    ],
    "start_dt": [
      "2021-04-21T00:01:00-04:00",
      "2021-04-21T00:01:00-04:00",
      "2021-04-21T00:01:00-04:00",
      "2021-04-21T00:01:00-04:00"
    ],
    "exp_dt": [
      "2022-04-21T00:01:00-04:00",
      "2022-04-21T00:01:00-04:00",
      "2022-04-21T00:01:00-04:00",
      "2022-04-21T00:01:00-04:00"
    ],
    "hrs_pwr": [
      "14",
      "12",
      "13",
      "15"
    ],
  
    "make_nm": "test",
    "model_nm": "test",
    "my_yr": "1980"
  }
]

"the length of list cannot not be expected and it could be more than 4 sometimes or less in some keys"

#Expected: i need to check if the above dictionary are in proper structure or not and based on that it should return the proper dictionary list associate with each item for eg:

def get_dict_list(items):
    if type(items == not structure)
        result = get_associated_dict_items_mapped
        return result
    else:
        return items
       

#Final result
expected_dict_list=
[{"dsply_nm":"test test","start_dt":"2021-04-21T00:01:00-04:00","exp_dt":"2022-04-21T00:01:00-04:00","hrs_pwr":"14"},

  {"dsply_nm":"test test","start_dt":"2021-04-21T00:01:00-04:00","exp_dt":"2022-04-21T00:01:00-04:00","hrs_pwr":"12","make_nm": "test",model_nm": "test","my_yr": "1980"},

  {"dsply_nm":"","start_dt":"2021-04-21T00:01:00-04:00","exp_dt":"2022-04-21T00:01:00-04:00","hrs_pwr":"13"},

  {"dsply_nm":"","start_dt":"2021-04-21T00:01:00-04:00","exp_dt":"2022-04-21T00:01:00-04:00","hrs_pwr":"15"}
]


in above dictionary payload, below part is associated with the second dictionary items and have to map respectively


   "make_nm": "test",
    "model_nm": "test",
    "my_yr": "1980"
  }

Can anyone help on this?

Thanks

2 answers

  • answered 2021-04-21 13:45 JonSG

    I think a couple of list comprehensions will get you going. If you would like me to unwind them into more traditional for loops, just let me know.

    import json
    
    def get_dict_list(item):
        first_value = list(item.values())[0]
    
        if not isinstance(first_value, list):
            return [item]
    
        return [{key: item[key][i] for key in item.keys()} for i in range(len(first_value))]
    
    cutomer_details = [
        {
            "insured_details": "asset",
            "id": "xxx",
            "dept": "account",
            "salary": "12"
        },
        {
            "insured_details": ["asset", "asset", "asset"],
            "id":["213","214","233"],
            "dept":["account","sales","market"],
            "salary":["12","13","14"]
        }
    ]
    
    cutomer_details_cleaned = []
    for detail in cutomer_details:
        cutomer_details_cleaned.extend(get_dict_list(detail))
    
    print(json.dumps(cutomer_details_cleaned, indent=4))
    

    That should give you:

    [
        {
            "insured_details": "asset",
            "id": "xxx",
            "dept": "account",
            "salary": "12"
        },
        {
            "insured_details": "asset",
            "id": "213",
            "dept": "account",
            "salary": "12"
        },
        {
            "insured_details": "asset",
            "id": "214",
            "dept": "sales",
            "salary": "13"
        },
        {
            "insured_details": "asset",
            "id": "233",
            "dept": "market",
            "salary": "14"
        }
    ]
    

  • answered 2021-04-21 19:59 itprorh66

    Since customer details is a list

    dict(zip(customer_details[0], list(customer_details.values[0]())))
    

    this yields:

    {'insured_details': ['asset', 'asset', 'asset'],
     'id': ['213', '214', '233'],
     'dept': ['account', 'sales', 'market'],
     'salary': ['12', '13', '14']}