Unpacking and testing nested dictionaries values python

matteo

I'm trying to transform a dictionaries of list and nested dictionaries into a cleaner list of dictionaries but testing one of the nested values.

Basically from this:

my_dict = {
    'N': [
        {'DATE': 2019, 'VALUE': 1}, 
        {'DATE': 2018, 'VALUE': 2}
    ], 
    'LT': [
        {'DATE': 2019, 'VALUE': 40}, 
        {'DATE': 2001, 'VALUE': 50}
    ]
}

to this:

my_dict_transformed = [
    {'DATE': 2019, 'N': 1, 'LT': 40}, 
    {'DATE': 2018, 'N': 2, 'LT': 'NULL'}, 
    {'DATE': 2001, 'N': 'NULL', 'LT': 50}
]

Thanks to all the solutions proposed. I think @Rakesh solution is the cleanest. While having a default function like @Anurag suggested could be very useful with a slightly different data structure.

Rakesh

This is one approach.

Ex:

my_dict = {
    'N': [
        {'DATE': 2019, 'VALUE': 1}, 
        {'DATE': 2018, 'VALUE': 2}
    ], 
    'LT': [
        {'DATE': 2019, 'VALUE': 40}, 
        {'DATE': 2001, 'VALUE': 50}
    ]
}

my_dict_transformed = {}
for k, v in my_dict.items():
    for i in v:
        if i["DATE"] not in my_dict_transformed:
            my_dict_transformed[i["DATE"]] = {'DATE': i["DATE"]}
            my_dict_transformed[i["DATE"]].update(dict((i,"NULL") for i in my_dict.keys()))
        my_dict_transformed[i["DATE"]][k] = i.pop('VALUE')
print(list(my_dict_transformed.values()))

Output:

[{'DATE': 2001, 'LT': 50},
 {'DATE': 2018, 'N': 2},
 {'DATE': 2019, 'LT': 40, 'N': 1}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unpacking nested list of dictionaries

Python - Nested Dictionaries - Extracting Values

python - iterating list of dictionaries and unpacking

Performance implications of unpacking dictionaries in Python

python: unpacking an array of dictionaries in a matrix

Python - Looping through nested dictionaries and fetching values

Compare keys and values of nested dictionaries in Python

Finding nested list with identical values in Dictionaries [Python]

Python: Looping over and combining values in nested dictionaries

Python sum values within nested dictionaries

Comparing values from nested dictionaries in Python 3

python: combine two nested dictionaries with dictionaries as values of the top level keys

Unpacking values in python

Unpacking a list of dictionaries based on key matches in Python

Python: Unpacking list containing multiple dictionaries

Matching values in nested dictionaries

Comparing Python dictionaries and nested dictionaries

Pythonically unpacking nested list values from a dictionary

Packing and unpacking float values in python

Python - how to get all the values of nested dictionaries as a single List[dict]?

Concept Question on calling values in nested dictionaries for Python3

Getting values from a nested list of dictionaries using python

Pulling specific values from nested dictionaries and arrays in JSON in Python

Python setting values for keys in nested dictionaries when keys are the same

How to combine (append values) two nested dictionaries with the same keys in python?

How to find the values of a key in a nested list of dictionaries in Python

Sort a list of nested dictionaries by values without knowing its keys [Python]

Sum up the values inside nested python dictionaries if they have the same key

Merging nested dictionaries in Python