Need Help Understanding Lists and Dictionaries


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/0/2?curriculum_id=4f89dab3d788890003000096


My code runs perfectly fine, I just want to understand how/why the computer processes this code. I'm having trouble understanding what the variable after the "for loop" is. I understand how everything else works, but confused about why the for loop variable (or whatever it is that "key" has been set to) is there. I'm not understanding its purpose.


webster = {
	"Aardvark" : "A star of a popular children's cartoon show.",
    "Baa" : "The sound a goat makes.",
    "Carpet": "Goes on the floor.",
    "Dab": "A small amount."
}

# Add your code below!
for key in webster:
    print webster[key]


#2

My apologies for being lazy. I decided instead of moving on I look back on earlier lessons. I have discovered my answer from https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/2/1?curriculum_id=4f89dab3d788890003000096 (Python List and Dictionaries - 10). I looked over the definition of Keys in a dictionary. A key is just the index, but in a dictionary because a dictionary can contain any string or number in its index. I hope this helps anyone with a similar problem. Please, correct me if I am wrong; I am entirely new to this.


#3

seems you still not entirely sure. a dictionary has keys (aardvark, baa, carpet and dab) this keys have values associated with them.

then your for loop:

for key in webster:

key will get assigned key from your webster dictionary.

you could name this variable anything, given you define it in the loop:

for example in webster:
    print webster[example]

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.