Tell us what’s happening:
Not sure what I’m missing, I’ve also tried
print(copper[‘age’], copper[‘food’], copper[‘species’]
Your code so far
# User Editable Region
copper = {
'species': 'guinea pig',
'age': 2
}
copper['food'] = 'hay'
copper['species'] = 'Cavia porcellus'
for coppper in copper:
print(copper['age'], copper['species'])
# User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Learn Algorithm Design by Building a Shortest Path Algorithm - Step 9
updating to add the instructions:
Step 9
To iterate over the keys of a dictionary, you can simply put the dictionary into a for
loop. The code below would print each key in the dictionary dict
:
Example Code
for i in dict:
print(i)
Replace the print()
call with a for
loop that iterates over copper
and prints each key.
for i in dict:
print(i)
See how this works, the loop iterates over the items in dict
. Each item is stored in the i
variable, so that’s what you print in the loop.
See how i
i the loop matches the i
in the print
for i in dict: # i variable created
print(i) # i variable used
prints each key
You have a key and a value in a dict {‘key’: ‘value’}
You want to print the key.
thank you!!! seeing {‘key’:‘value’} as you displayed was very helpful
1 Like