You can use OrderedDict (Python 2.7 required) or higher.
Also note that OrderedDict({'a': 1, 'b':2, 'c':3}) will not work, since the dict created with {...} has already forgotten the order of the elements. Instead, you want to use OrderedDict([('a', 1), ('b', 2), ('c', 3)]) .
As mentioned in the documentation, for versions below Python 2.7 you can use this recipe.
Abhinav Gupta Apr 12 2018-11-11T00: 00Z
source share