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:

{'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)

>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  

Leave a Reply