I'm new to working with dictionaries in python and have been stuck on how I can iterate through and reference a specific key within a nested dictionary. For instance, in the following code, I'm looking to list out only the pet names for each pet in the dictionary. Right now, I have all of the keys in each pet being listed out.
petDict = {
'pet1' : {
'type' : 'dog',
'name' : 'scooby',
'age' : '4'
},
'pet2' : {
'type' : 'cat',
'name' : 'milo',
'age' : '1'
},
'pet3' : {
'type' : 'fish',
'name' : 'danny',
'age' : '2'
}
}
print('Here are my pets:')
for petNum, petInfo in petDict.items():
for key in petInfo:
print(key + ': ', petInfo[key])
current output:
type: dog
name: scooby
age: 4
type: cat
name: milo
age: 1
type: fish
name: danny
age: 2
desired output:
name: scooby
name: milo
name: danny