collections.OrderedDict
is a subclass of dict
that remembers in what order its entries were added. >>> import collections >>> od = collections.OrderedDict( dict( one='x' , two='q' , three='s' )) >>> od['four'] = 'a' >>> od['five'] = 't' >>> for k, v in od.items(): print(f'{k} = {v}') ... one = x two = q three = s four = a five = t >>>