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 Introduction To Dictionaries

Elaboration on the order in which the keys are saved in?

I've been following along using the Python 3.4 terminal (It's quicker than using the workspace when I'm learning how things work and syntax). I punched in the same my_dict dictionary containing a "name" and a "job" and noticed that when calling it, it did not change the order as it did in the video. I know it was mentioned the order in which it is saved can change over time, but why is that? If I call it several times over, say, a loop, will the order change? Is this important to delve into?

1 Answer

Dan Johnson
Dan Johnson
40,532 Points

Repeated iterations over a dictionary's keys (where nothing is modified) shouldn't change order over the duration of a program. However for some types each new instance of the Python interpreter can result in different hash values, and since dictionaries are implemented as hashtables this can change their order.

Reasons for the randomization

Either way you don't want to rely on that order. If you do need the insertion order remembered, you can use an OrderedDict