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.
>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