Right answer but not in an expected order


#1




I got the results using code as below and passed this lesson, but the order is a-c-b instead of a-b-c which shows in the dictionary. I am wondering why the loop changes the order here.

a apple
c cherry
b berry
None


d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
    # Your code here!
    print key, d[key]


#2

dictionary's are not ordered. So no, the loop doesn't change the change the order.


#3

Thanks for the answer. One more question:
Every time I run the code the results donot change. Does that mean the order of a dictionary is not the order we put in but it has its own order when stored?


#4

why is this important? The honest answer, i don't know. Python dictionary's are hash mapping/hash table, you can read about this, and then see how this implemented in python, but is that really so important?


#5

It is not. Just out of curiosity. :sweat_smile:


#6

well, i can't satisfy your curiosity, because i don't know.


#7

Hi @chipblaster70195,

Since a Python dictionary is a hash table, you can Google that term to find lots of information about them, including how items are stored.

See:


#8

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