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 specific key values from a list of dictionaries in Python?

I am trying to print specific key value from a list of dictionaries in Python, list is as following:

employees = [
    {'name': 'Tanya', 'age': 20, 'birthday': '1990-03-10',
        'job': 'Back-end Engineer', 'address': {'city': 'New York', 'country': 'USA'}},
    {'name': 'Tim', 'age': 35, 'birthday': '1985-02-21', 'job': 'Developer', 'address': {'city': 'Sydney', 'country': 'Australia'}}]

How can print out specific specific key values and loop through each dictionary? Output should be Name, job and city.

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

>Solution :

Possible solution is following:

employees = [
    {'name': 'Tanya', 'age': 20, 'birthday': '1990-03-10',
        'job': 'Back-end Engineer', 'address': {'city': 'New York', 'country': 'USA'}},
    {'name': 'Tim', 'age': 35, 'birthday': '1985-02-21', 'job': 'Developer', 'address': {'city': 'Sydney', 'country': 'Australia'}}]

for employee in employees:
    print(f"{employee['name']}, {employee['job']}, {employee['address']['city']}")

Prints

Tanya, Back-end Engineer, New York
Tim, Developer, Sydney
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