Right answer but not in an expected order


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

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

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


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


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?


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?


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


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


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.



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