Python Forum
i keep getting unordered output when using dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i keep getting unordered output when using dictionaries (/thread-20645.html)



i keep getting unordered output when using dictionaries - fuadfzy - Aug-23-2019

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


RE: i keep getting unordered output when using dictionaries - fishhook - Aug-23-2019

Dictionaries do not keep the order. If you really need to keep order use OrderedDict instead.


RE: i keep getting unordered output when using dictionaries - perfringo - Aug-23-2019

(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)


RE: i keep getting unordered output when using dictionaries - DeaD_EyE - Aug-23-2019

Final thoughts: You're using the wrong Python version.


RE: i keep getting unordered output when using dictionaries - fishhook - Aug-23-2019

I would highly recommend not building your algorithms based on the assumption of dictionary order.