Python Forum
i keep getting unordered output when using dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i keep getting unordered output when using dictionaries
#1
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
Reply
#2
Dictionaries do not keep the order. If you really need to keep order use OrderedDict instead.
Reply
#3
(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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Final thoughts: You're using the wrong Python version.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
I would highly recommend not building your algorithms based on the assumption of dictionary order.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020