0

I have recently been working on a python application that handles some sort of schedule. I have a dictionary that contains the number of days in a rotation in a schedule, and then each day contains a dictionary with each different part of the day. It looks like this:

schedule = {
'rotation': 6,
'1' : {'B': '8:32', 'C': '9:34', 'D' : '10:36', 'F':'12:11', 'G': '1:13', 'H':'2:15'},

'2' : {'A': '8:32', 'B': '9:34', 'C,' : '10:36', 'E':'12:11', 'F': '1:13', 'G,':'2:15'},

'3' : {'A': '8:32', 'B': '9:34', 'D,' : '10:36', 'E':'12:11', 'F': '1:13', 'H,':'2:15'},

'4' : {'A': '8:32', 'C': '9:34', 'D,' : '10:36', 'E':'12:11', 'G': '1:13', 'H,':'2:15'},


'5' : {'B' : '8:40', 'D' : '11:00', 'F' : '12:55', 'H' : '2:15' },

'6' : {'A' : '8:40', 'C' : '11:00', 'E' : '12:55', 'G' : '2:15' }

}

This all looks like it should work, yet when I print it out, I get a distorted dictionary that looks like it is sorted:

{'1': {'C': '9:34', 'B': '8:32', 'D': '10:36', 'G': '1:13', 'F': '12:11', 'H': '2:15'}, 
'3': {'A': '8:32', 'D,': '10:36', 'B': '9:34', 'E': '12:11', 'F': '1:13', 'H,': '2:15'}, 
'2': {'A': '8:32', 'B': '9:34', 'E': '12:11', 'F': '1:13', 'C,': '10:36', 'G,': '2:15'}, 
'5': {'H': '2:15', 'B': '8:40', 'D': '11:00', 'F': '12:55'}, 
'4': {'A': '8:32', 'C': '9:34', 'E': '12:11', 'G': '1:13', 'D,': '10:36', 'H,': '2:15'}, 
'6': {'A': '8:40', 'C': '11:00', 'E': '12:55', 'G': '2:15'}, 
'rotation': 6}

As you can see, in day 1, it starts with C instead of B when printing, and the 'rotation' is at the end of the dictionary instead of the front. Why does my dictionary print like this?

Nick D
  • 590
  • 3
  • 10
  • 21
  • Dictionaries are generally unordered. You can't expect your insertion order to be preserved. – user2390182 Oct 08 '18 at 14:58
  • 1
    This question could help https://stackoverflow.com/questions/11784860/why-does-this-python-dictionary-get-created-out-of-order-using-setdefault – Alex Ford Oct 08 '18 at 14:58

3 Answers3

1

The order in a dictionary is not stable, due to the hash function. On top of this, Python now uses a salt value when hashing, meaning that the order will be different each run (except if you ask for a stable dict).

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

Python dictionary is not required to preserve order. If order is what you want then you could use lists. If you just want to view a dictionary in sorted order, you can use .sort() or sorted() to help you print.

Jake
  • 617
  • 1
  • 6
  • 21
0

You can sort this dictionary but you have to make an exception for rotation since its values do not fit the rest of the format of being a dictionary with alphabetical keys

d = {k: dict(sorted(v.items(), key=lambda x: x[0])) if k != 'rotation' else schedule[k] for k, v in schedule.items()}
print(d)
# {'rotation': 6, '1': {'B': '8:32', 'C': '9:34', 'D': '10:36', 'F': '12:11', 'G': '1:13', 'H': '2:15'}, '2': {'A': '8:32', 'B': '9:34', 'C,': '10:36', 'E': '12:11', 'F': '1:13', 'G,': '2:15'}, '3': {'A': '8:32', 'B': '9:34', 'D,': '10:36', 'E': '12:11', 'F': '1:13', 'H,': '2:15'}, '4': {'A': '8:32', 'C': '9:34', 'D,': '10:36', 'E': '12:11', 'G': '1:13', 'H,': '2:15'}, '5': {'B': '8:40', 'D': '11:00', 'F': '12:55', 'H': '2:15'}, '6': {'A': '8:40', 'C': '11:00', 'E': '12:55', 'G': '2:15'}}
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20