Looping over a dictionary, wrong order


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/1?curriculum_id=4f89dab3d788890003000096


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]


#2

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.


#3

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