Python Forum

Full Version: i keep getting unordered output when using dictionaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys, i'm new here. please go easy on me Smile

i got a quite dummy question to ask. when i used the dictionaries data structure, i keep getting the output randomly ordered. this is the example of it:

content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}

print(content_ratings)
and the output will be:

Output:
{'9+': 987, '4+': 4433, '12+': 1155, '17+': 622}
i don't know how the index are not the same with the way i write the code. it's quite confusing. thank you in advance guys
Dictionaries do not keep the order. If you really need to keep order use OrderedDict instead.
(Aug-23-2019, 10:02 AM)fishhook Wrote: [ -> ]Dictionaries do not keep the order. If you really need to keep order use OrderedDict instead.

This is not totally correct. Starting from Python 3.6 dictionaries are 'insertion ordered' and from Python 3.7 it's guaranteed. Obviously OP is not using recent versions of Python (Python 3.6 was released on December 23, 2016)
Final thoughts: You're using the wrong Python version.
I would highly recommend not building your algorithms based on the assumption of dictionary order.