11/4 - For the record


#1

I have been using this solution and successfully displayed the information, still it's been providing error. Anyone can provide alternative solution or can amend this code.

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 key in students:
if key==lloyd:
print "Lloyd"
print key
elif key == alice:
print "Alice"
print key
elif key == tyler:
print "Tyler"
print key


#2

Whew, that's a lot of work! And it is anything but D.R.Y.. That's okay. You've got a good start with the for statement:

for i in range(len(students)):
    print students[i]['name']
    for key in students[i]:
        print "%s: %s" % (key, students[i][key])

Because students is an array, we must iterate over it with a numeric index (range). With each iteration we can enumerate the dictionary in that array member like any dictionary.


#3

@mtf Thanks buddy!! i will keep in mind about D.R.Y & ya "for" loop stuff i did it too far, i guess i m starting getting hold of something in programming hahhaha

Again thanks a lot !!


#4

This solution worked best for me:

students = [lloyd, alice, tyler] #omitted original libraries above
    
for key in students:
        print key["name"]
        print key["homework"]
        print key["quizzes"]
        print key["tests"]

It prints the same results as a code similar to your original, longer code. I'm not sure what approach would be best for other scenarios, but yeah. Looking at both of your suggestions helped me solve it!


#5

I'm not sure which approach is best, either. Since all we are doing is printing the contents, we have a number of approaches..


#6

yes, i made it the same way

students = [lloyd, alice, tyler]

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


#7

It's interesting how many different ways you can do it.

My code was accepted, but it didn't return everything in order. It started with Tyler's last two results and then went onto Lloyd and everything in order from there. Still trying to figure out why.

students = [lloyd, alice, tyler]
for i in students:
    for key in i:
        print i[key]

#10

so i'm curious why a response couldn't be...

for i in students:
    print students [0:3]

where for each iteration, you print off all the data for each of the lists within the student's individual dictionary... unless it has to be arranged and not just printed off


#11

We'll need a guru to weigh in here... I believe that for..in does not necessarily follow the order of the data structure unless we use range(), in which case the iterator is an integer, not an item (as in list object).

If we want to specify order,

for i in range(len(students)):
    student = students[i]
    for key in student:
        print student[key]