0
testVals = {1001: "AV", 1002: "AV", 1003: "AV", 1004: "AV", 1005: "BV", 1006: "BV", 1007: "BV", 1008: "BV"}
print testVals

This prints: {1008: 'BV', 1001: 'AV', 1002: 'AV', 1003: 'AV', 1004: 'AV', 1005: 'BV', 1006: 'BV', 1007: 'BV'}

What is going on here? Why is the last one indexed at 0 now? I need them to be in order. thanks

golu
  • 629
  • 2
  • 7
  • 18
  • 1
    Python `dict` is inherently unordered (except for Python 3.6 currently) – dawg May 08 '17 at 22:15
  • 1
    http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key was the best answer to this frequent duplicate question that I found in a quick search. – Peter DeGlopper May 08 '17 at 22:15
  • Thanks that makes sense. I guess I should use tuple for my purpose then. – golu May 08 '17 at 22:18
  • 1
    Unless you give us context as to why you need it ordered, it would be quite hard to help you make the best choice. One such choice available to you is an [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict) – Olivier May 08 '17 at 22:22
  • 1
    You're asking basically two things: "Why is it displayed like that" and "How to keep them in order" so I closed your question as duplicate to two questions that deal with these questions. If you think the duplicates don't solve your question: [edit] the question and explain why it's not a duplicate. :) – MSeifert May 08 '17 at 22:28

0 Answers0