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

How to print multilevel nested dictonary in python

Here is my code

     print(data['a'][0]['aa'])
    print(data['a'][0].keys())

This is input->

    data={
        'a':[{
            'aa':{'aax':5,'aay':6,'aaz':7},
            'ab':{'abx':8,'aby':9,'abz':10}
            },
            {
            'aaa':{'aaax':11,'aaay':12,'aaaz':13},
            'aab':{'aabx':14,'aaby':15,'aabz':16}
            }]
    }
    
    
   

How can i print the dictionary like this output

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:
    
    Key:aax Value: 5
    Key:aay Value: 6
    Key:aaz Value: 7
    Key:abx Value: 8
    Key:aby Value: 9
    Key:abz Value: 10
    Key:aaax Value: 11

How can i loop through in this type of data.How can i loop through and print all the data I can access the single data but how can print all data.

>Solution :

To print all the key-value pairs in a multilevel nested dictionary, you can use a nested loop structure. Here is an example:

for outer_dict in data['a']:
    for inner_dict in outer_dict.values():
        for key, value in inner_dict.items():
            print(f"Key: {key} Value: {value}")
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