For the record


#1

Hi, regarding the printing part, why doesn't the code below work (I know what does work); just being curious here


for item in students:
    print item[0]
    print item[1]
    print item[2]
    print item[3]


#2

Read the instructions carefully. If it says FOR in a box, it means you have to use this in your code.

students = [lloyd, alice, tyler]

for student in students:
print student["name"]
print student["homework"]
print student["quizzes"]
print student["tests"]


#3

Thanks, but that doesn't answer my question. What I'm wondering is, without any context, why can't a key in a dictionary be called in the same way you can call an index in a list? "Name" is the first key in my dictionary, so why can't I replace it with dictionary[0]?


#4

I'm still a rookie but my guess would be that a dictionary consists of words. A list however is build on numbers.


#5

Actually a list can also have words in it (as in students = ["lloyd", "alice", "trump"]), or isn't that what you meant?

I've figured it out - it has to do with the fact that when you do dictionary[0], it looks for the key 0 in dictionary, not index 0. And if you haven't specified that key, then it doesn't find anything.
When you go:
some_dictionary = {
0 : "Text 1",
"Alice": "Text 2",
"Trump": "Text 3"
}
- then print some_dictionary[0] prints "Text 1" :slight_smile:


#6

is there any mistake if I use

students=[lloyd,alice,tyler]
for i in students:
print(i["names"])
print(i["homework"])
print(i["quizzes"])
print(["tests"])


#7

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