Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Dictionary Iteration

Chih-Wei Hsu
Chih-Wei Hsu
11,132 Points

why does print return the same order to which key-value pairs have been first established in a dictionary?

If dictionary has no order, I'd assume that the order to which a key or its value is returned or printed would be random when using a for loop. However in the video, Kenneth seems to get the same order to which the key/value pairs have been entered in the dictionary. He also mentions something about the order remaining constant until the dictionary is changed, but I still don't quite understand what he means.

1 Answer

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

You probably have to watch the video again because the dictionary he printed after he declared dictionary was unordered. For example, he put age as 2nd key but when he printed it was on 4th.

Python will internally organize dictionary keys on its favourable way. All you need to know is python dictionary is unordered. If you are really curious, query hash table implementation on Google.

Chih-Wei Hsu
Chih-Wei Hsu
11,132 Points

you are right. the dictionary did print out in another order from how he first declared it, but each of the for loops that he calls iterates through the dictionary in the same order. if i understand you correctly about python's internal way of organizing dictionaries, python does give it an order of some kind until the dictionary is modified in some way (as kenneth has mentioned)??

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Once Python organizes keys on its term that order is not redefined unless you modify them such as adding another key. So by the time Kenneth called it on for loop, the dictionary is organised internally by Python's hashing algorithm. You can try this with simple code examples.

a = {'a': 1, 'b': 2, 'c': 3}
b = {'b': 2, 'c': 3, 'a': 1}
c = {'c': 3, 'a': 1, 'b': 2}

print(a, b, c) 
#({'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2})

print(a, b, c) 
#({'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2})

So you can see Python has its consistent way of organizing keys but you cannot predict it.

I don't understand when you say dictionary keys are ordered when it went through for loop. Python organizes and keep them as they are it doesn't shuffle around. But once you modified the key you won't know about how it is organized.