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

Combining dictionaries with a particular key: Python

I have the following dictionaries generated using for loop and I would to combine and dump them into one YAML file.

dict1

{'name': 'test',
 'version': '0011 * *? *',

 'datasets': [{
   'dataset': 'dataset_a',
   'match_on': ['code'],
   'columns': {'include': ['aa',
     'bb',
     'cc']}}]
}

dict2

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

{'name': 'test',
 'version': '0011 * *? *',

 'datasets': [{
   'dataset': 'dataset_b',
   'match_on': ['code'],
   'columns': {'include': ['aaa',
     'bbb',
     'ccc']}}]
}

The dictionaries have common values on name and version keys. So, I would like to put it as common but they have differences on the datasets key. The desired output look like the following

combined_dict

{'name': 'test',
 'version': '0011 * *? *',

 'datasets': [{
   'dataset': 'dataset_a',
   'match_on': ['code'],
   'columns': {'include': ['aa',
     'bb',
     'cc']}}]

'datasets': [{
   'dataset': 'dataset_b',
   'match_on': ['code'],
   'columns': {'include': ['aaa',
     'bbb',
     'ccc']}}]
}

To achieve this, I have tried the following.

combined_dict= {**dict1,**dict2}

with open('combined_dict.yml', 'w') as outfile:
    yaml.dump(updated_dict , outfile, sort_keys=False)

>Solution :

As said in the comment, you cannot do what you want since the key in the dictionary has to be unique. What you can do is add an element to the list associated with the key "datasets", in order to obtain something like this:

{
    "name": "test",
    "version": "0011 * *? *",
    "datasets": [
        {
            "dataset": "dataset_a",
            "match_on": ["code"],
            "columns": {"include": ["aa", "bb", "cc"]},
        },
        {
            "dataset": "dataset_b",
            "match_on": ["code"],
            "columns": {"include": ["aaa", "bbb", "ccc"]},
        },
    ],
}

This is the code to do that:

combined_dict = dict1.copy()
combined_dict["datasets"].extend(dict2["datasets"])

If you want to modify in-place dict1, just replace combined_dict with dict1

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