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

Mapping of Keys using dictionary and lists in Python

I have a list of list of dictionary like below:

ini_dict = [[{'nikhil': 1, 'vashu': 5,
              'manjeet': 10, 'akshat': 16}, {'nikhil': 1, 'vashu': 5,
                                             'manjeet': 10, 'akshat': 15}]]  

I have modified and reduced one level like below:

mod = []
for i in ini_dict:
    for j in i:
        mod.append({item: values for item, values in j.items()}) 

This gives me output as below:

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

[{'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}, {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}]

Now, I have another set of Keys dictionary using which the keys of the above data should be replaced by it.

result_dict1 = {"nikhil": "Nikhil", "vashu": "Vashu", "manjeet": "Manjeet", "akshat": "Akshat"}  

Now, I have tried the below code but it’s not giving me the output I need:

final = []
for j, k in result_dict1.items():
    for i in mod:
        for x, y in i.items():
            final += [{k: y}]  

Output:

[{'Nikhil': 1}, {'Nikhil': 5}, {'Nikhil': 10}, {'Nikhil': 15}, {'Nikhil': 1}, {'Nikhil': 5}, {'Nikhil': 10}, {'Nikhil': 15}, {'Vashu': 1}, {'Vashu': 5}, {'Vashu': 10}, {'Vashu': 15}, {'Vashu': 1}, {'Vashu': 5}, {'Vashu': 10}, {'Vashu': 15}, {'Manjeet': 1}, {'Manjeet': 5}, {'Manjeet': 10}, {'Manjeet': 15}, {'Manjeet': 1}, {'Manjeet': 5}, {'Manjeet': 10}, {'Manjeet': 15}, {'Akshat': 1}, {'Akshat': 5}, {'Akshat': 10}, {'Akshat': 15}, {'Akshat': 1}, {'Akshat': 5}, {'Akshat': 10}, {'Akshat': 15}]  

Keys are replaced but the combination is 1:N.

Expected output:

[{'Nikhil': 1, 'Vashu': 5, 'Manjeet': 10, 'Akshat': 15}, {'Nikhil': 1, 'Vashu': 5, 'Manjeet': 10, 'Akshat': 15}]

>Solution :

You don’t seem to check if a key of the inner dictionary in mod matches the key in the replacement dict, that’s an issue.

A list comprehension with a dict comprehension does the result:

>>> [{result_dict1[key]: value for key, value in inner_dict.items()}
     for inner_dict in mod]

[{'Nikhil': 1, 'Vashu': 5, 'Manjeet': 10, 'Akshat': 15},
 {'Nikhil': 1, 'Vashu': 5, 'Manjeet': 10, 'Akshat': 15}]

So the dict comprehension is changing the inner dicitionaries of the mod such that the keys are replaced from result_dict1. The list comprehension helps do this for every inner dictionary.

This will err if result_dict1 isn’t comprehensive enough.

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