Confused!


#1


Hi...I just practicing Exercise 11->4(For the Record)....and I wrote this code and it comez up with an very interesting output.....
https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/4?curriculum_id=4f89dab3d788890003000096

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students=[lloyd, alice, tyler]
for n in students:
    print lloyd["name"]
    print alice["name"]
    print tyler["name"]

OUTPUT

Lloyd
Alice
Tyler
Lloyd
Alice
Tyler
Lloyd
Alice
Tyler
None

My question is....can we assign dictionary to the list item and if yes how it will work and where?

Please Help!!!!!!


#2

Because the loop runs through three iterations. Try using the dynamic variable,

for student in students:
    print student["name"]

#3

Yes !!!!! got it..pretty easy.... Stupid Question.......Did you see my 1st question. can you help...I am very confused about that


#4

We cannot see your lessons, which is why we ask people to post their code. Go ahead and post it here, along with a link to the exercise so we can test your code.


#5

NO need to go through the chepter....

My question is....can we assign dictionary to the list item and if yes how it will work and in what type of scenario we can use this code......

students=[lloyd, alice, tyler]

where lloyd, alice & tyler are dictionary........
Please Help!!!!!!


#6

lloyd, alice, tyler are dictionary references. When we put them in a list, the data stays where it is, and the list contains only the references to their location.

for student in students:
    print student['name']

will output,

Lloyd
Alice
Tyler

as that is the name attribute on those referenced objects.


#7

Got it......Thank you


#8

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