I have an OrderedDict like this:
od = OrderedDict({
'a':{'1-k1':'v1',
'2-k2':'v2',
'3-k3':'v3',
},
'b':{'4-k4':'v4'},
{'5-k5':'v5'},
{'6-k6':'v6'},
})
And I wanted to get a list of all keys in one of the second-level dictionaries, so I did:
aod = OrderedDict(od.get('a'))
a_message = ''
for a_key in list(aod.keys()):
a_message = amessage + a_key + ' \n'
print (a_message)
Now here's the thing, when I run this in a local script, it outputs:
"
1-k1
2-k2
3-k3
"
But, when I integrated it in my Messenger Chatbot using Heroku, the list isn't in order. I thought there might be something interfering in the app script, so I put that block of code at the top and printed it right after it finished, and it was still unordered. I also tried making the inner dictionaries Ordered too.
So what would cause an OrderedDict list, that's working locally, to be unordered after deployment?
The ordering in your local machine is mere happenstance. The nested dicts are vanilla dicts, and the idea of ordering is not applicable.
You need to make the nested dicts instances of OrderedDict to get the ordering you want:
od = OrderedDict([
('a': OrderedDict(...)),
...
])
You should change the data structure, and not call OrderedDict
on a dictionary, as that does not guarantee the order you want