UPDATE
Starting with Python 3.7, dictionaries remember the insertion order . By simply adding a new value, you can be sure that it will be βat the endβ if you iterate over the dictionary.
Dictionaries have no order and, therefore, have no beginning or end. The display order is arbitrary. If you need order, you can use list tuple instead of dict :
In [1]: mylist = [] In [2]: mylist.append(('key', 'value')) In [3]: mylist.insert(0, ('foo', 'bar'))
You can easily convert it to a dict later:
In [4]: dict(mylist) Out[4]: {'foo': 'bar', 'key': 'value'}
Alternatively, use collections.OrderedDict as suggested by IamAlexAlright.
source share