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

Generate list of values from nested dictionary as values

I have a nested dictionary like this:

d = {'A': {'A': 0.11, 'C': 0.12, 'D': 1.0}, 'B': {'B': 0.13, 'C': 0.14}}

I want to generate this output where in values only the list of keys of inner dictionary are selected.

output dictionary

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

output = {'A':['A', 'C', 'D'], 'B':['B', 'C']}

Is there any way to do?

>Solution :

You can pass each sub-dict to the list constructor to convert the keys to a list:

output = {k: list(v) for k, v in d.items()}
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