Looping over a dictionary, wrong order



The code works almost perfectly, except that it prints:

a apple
c cherry
b berry

rather than

a apple
b berry
c cherry

Which is just... strange, I can't see any reason why it wouldn't go in order from left to right as I assume that's how the code is read. Any suggestions?

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

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


I can't see any reason why it would!
When you use a data structure or function or just about anything, only rely on the behavior that they promise, anything else that they may or may not be doing is undefined behavior.


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