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

Is there a way to extract the selected value in a nested Dictionary using a for loop?

Using this dictionary, is there a way I can only extract the Name, Last Name, and Age of the boys?

myDict = {'boy1': {'Name': 'JM', 'Last Name':'Delgado', 'Middle Name':'Goneza', 'Age':'21', 
        'Birthday':'8/22/2001', 'Gender':'Male'},
        'boy2': {'Name': 'Ralph', 'Last Name':'Tubongbanua', 'Middle Name':'Castro', 
        'Age':'21', 'Birthday':'9/5/2001', 'Gender':'Male'},}

for required in myDict.values():
    print (required ['Name', 'Last Name', 'Age'])

The output is:

JM
Ralph

What I have in mind is

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

JM Delgado 21
Ralph Tubongbanua 21

>Solution :

You have to extract the keys one by one:

myDict = {'boy1': {'Name': 'JM', 'Last Name':'Delgado', 'Middle Name':'Goneza', 'Age':'21', 
        'Birthday':'8/22/2001', 'Gender':'Male'},
        'boy2': {'Name': 'Ralph', 'Last Name':'Tubongbanua', 'Middle Name':'Castro', 
        'Age':'21', 'Birthday':'9/5/2001', 'Gender':'Male'},}

for required in myDict.values():
    print (required['Name'], required['Last Name'],required['Age'])
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