Combining dictionaries with a particular key: Python

Question:

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

{'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)
Asked By: Hiwot

||

Answers:

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

Answered By: Domiziano Scarcelli
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.