Skip to content Skip to sidebar Skip to footer

Flatten Nested Dictionary To Key And Joined String Value

I need help with a function to flatten a nested dictionary in the following format: dict_test = { 'id' : '5d4c2c0fd89234260ec81', 'Reference Number' : 'JA-L800D-191', '

Solution 1:

You can use join() built-in method to join values together.

def do():
    dict_test = {
        "id": "5d4c2c0fd89234260ec81",
        "Reference Number": "JA-L800D-191",
        "entities_discovered": {
            "OTHER_ID": [
                "L800DFAG02191"
            ],
            "CODE_ID": [
                "160472708",
                "276954773"
            ]
        },
        "label_field": [
            "ELECTRONICS",
            "HDMI"
        ],
        "numeric_field": [
            491,
            492
        ],
    }

    new_dict = {}
    for key, value in dict_test.items():
        if isinstance(value, dict):
            for _key, _value in value.items():
                if isinstance(_value, list):
                    new_dict.update({_key: ', '.join([str(item) for item in _value])})

        elif isinstance(value, list):
            new_dict.update({key: ', '.join([str(item) for item in value])})

        else:
            new_dict.update({key: value})

    return new_dict


if __name__ == '__main__':
    print(do())

Output:

{
    'id': '5d4c2c0fd89234260ec81',
    'Reference Number': 'JA-L800D-191',
    'OTHER_ID': 'L800DFAG02191',
    'CODE_ID': '160472708, 276954773',
    'label_field': 'ELECTRONICS, HDMI',
    'numeric_field': '491, 492'
}


Solution 2:

def recursive_flatten_dict(tmp, dict_test):
    for i,v in dict_test.items():
        if type(v) == type({}):
            recursive_flatten_dict(tmp,v)
        else:
            tmp[i] = v
    return tmp

recursive_flatten_dict({},dict_test)

Solution 3:

Simple recursion using a generator:

def flatten(d):
   for a, b in d.items():
     if isinstance(b, dict):
        yield from flatten(b)
     else:
        yield (a, b if not isinstance(b, list) else ', '.join(map(str, b)))


print(dict(flatten(dict_test)))

Output:

{
 'id': '5d4c2c0fd89234260ec81', 
 'Reference Number': 'JA-L800D-191', 
 'OTHER_ID': 'L800DFAG02191', 
 'CODE_ID': '160472708, 276954773', 
 'label_field': 'ELECTRONICS, HDMI', 
 'numeric_field': '491, 492'
}

Solution 4:

def flatten(dict_test): 
    for key in ['label_field', 'numeric_field']: 
        dict_test[key]= ', '.join([str(c) for c in dict_test[key]])

    for c in dict_test['entities_discovered'].keys(): 
        dict_test[c]= ', '.join(dict_test['entities_discovered'][c])

    return dict_test

The above function does the job. I hope this what you are looking for?


Post a Comment for "Flatten Nested Dictionary To Key And Joined String Value"