Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging and flattening two lists of dictionaries using keys as new fields

I have two lists of dictionaries, each with the same structure. I wish to flatten into a single dictionary taking precedence of list 2, taking the value as the key of a new flat dictionary.

The following code works, but it feels like it’s hacking together code that can probably be done via one or two simple comprehensions. Is there a better way than this?

It produces this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'SourceIP': 'src2', 
 'DestinationIP': 'dst', 
 'Direction': 'dir', 
 'NEW': 'newvalue'
}

Here is the code:

import operator

default = [
    {"RealField": "SourceIP",   "SuppliedField": "src"},
    {"RealField": "DestinationIP", "SuppliedField": "dst"},
    {"RealField": "Direction", "SuppliedField": "dir"}
]

product_mapping = [
    {"RealField": "SourceIP",   "SuppliedField": "src2"},
    {"RealField": "DestinationIP",   "SuppliedField": "dst2"},
    {"RealField": "NEW",   "SuppliedField": "newvalue"},
]


def dictionary_from_mappings(default_mapping, product_mapping):
    default = [{i["RealField"]:i["SuppliedField"]} for i in default_mapping]
    default_flat = reduce(operator.ior, default, {})
    
    product = [{i["RealField"]:i["SuppliedField"]} for i in product_mapping]
    product_flat = reduce(operator.ior, product, {})
    return default_flat | product_flat

mappings = dictionary_from_mappings(default, product_mapping)
print(mappings)

>Solution :

You can use dict comprehensions to create the flats directly, like this:

def dictionary_from_mappings(default_mapping, product_mapping):  
    default_flat = {i["RealField"]: i["SuppliedField"] for i in default_mapping}  
    product_flat = {i["RealField"]: i["SuppliedField"] for i in product_mapping}
    return default_flat | product_flat  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading