If you want to control the order of items in a dictionary, you can use an OrderedDict. It preserves the original insertion order of data. It’s a useful construct for when you want to seriealize or encode in different formats. As a note, the structure of an OrderedDict (a doubly linked list), means that these dictionaries are at least twice as heavy as normal dictionaries, meaning they require more memory. A regular dictionary does not track the insertion order, and iterating over it produces the values in an arbitrary order. In an OrderedDict, the order the items are inserted is remembered and used when creating an iterator.

import collections

print('Regular dictionary:')
## Regular dictionary:
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k, v in d.items():
    print(k, v)
## a A
## b B
## c C
## d D
## e E
print('\nOrderedDict:')
## 
## OrderedDict:
d = collections.OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k, v in d.items():
    print(k, v)
## a A
## b B
## c C
## d D
## e E