Dictionary starts printing from halfway through


#1

Student becomes the teacher 4

The Dictionary are printing from halfway through.

I would expect the Dictionaries to print from the top and end at the bottom. Is this just how python works or is it a problem with my 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 student in students:
    for info in student:
        print info, student[info]

This is how it's printing:

quizzes [88.0, 40.0, 94.0]
tests [75.0, 90.0]
name Lloyd
homework [90.0, 97.0, 75.0, 92.0]
quizzes [82.0, 83.0, 91.0]
tests [89.0, 97.0]
name Alice
homework [100.0, 92.0, 98.0, 100.0]
quizzes [0.0, 75.0, 78.0]
tests [100.0, 100.0]
name Tyler
homework [0.0, 87.0, 75.0, 22.0]
None

The code still lets me pass the lesson I am just confused as to why this is happening.


#2

Top? Bottom? Of what? Dictionaries? Dictionaries do not represent order, there is no top or bottom, that's just not how the data is organised. Have a look at wikipedia's article on hash tables (or some other source obviously)


#3

Is there any way to get the library to print in the order that it appears in the code?


#4

What is a library?
Do you know of a data structure that stores values in sequence?


#5

I believe codecademy calls these libraries


#6

I'd hope not.

>>> type({})
<class 'dict'>

Library in this context would more likely refer to modules like math or datetime


#7

replace library with dictionary


#8

Sure, but if you mean that I should re-answer your question about how to keep order, I already did that! (With a question)


#9

Noticed you updated the title, are you giving up on me?
I won't provide you with any conclusions, but you're very welcome to pick my brain about what's going on - the reasoning leading to our knowledge is more important than the knowledge itself.

What you have so far is:
- dict is a hash table, and that is not organized in a way that represents order
- are there data structures that do represent order?

If you're confused about why a hash table does not represent order, then do a little brief research to get a general idea of what they do -- they are extremely valuable, they are even used to map variable names to their values in a lot of situations in Python, so they are used all the time


#10

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