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

Accessing nested dictionary contains list in python

I want only values from list inside nested dictionary.
Please advise whether I can print it directly rather than using a for loop.

dict_st={
  "students": [
    {
      "firstName": "Alex",
      "lastName": "Rodriguez"
    },
    {
      "firstName": "David",
      "lastName": "Crosby"
    },
    {
      "firstName": "Anna",
      "lastName": "Weston"
    }
  ]
}

for key, value in dict_st.items(): 
    for key1 in value:
        print ( key1["firstName"] )

OUTPUT:

Alex
David
Anna

I am looking for doing it one liner print statment something like below. Is this possible?

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

print ( dict_st["students"][0] )

OUTPUT:

{'firstName': 'Alex', 'lastName': 'Rodriguez'}

I am looking only "Alex" to be printed in single print statement.

>Solution :

It’s easy:

print ( dict_st["students"][0]["firstName"] )
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