According to my understanding, python dictionary does not maintain the insertion order. For example,
In [1]: dict = {'Name': 'Zara', 'Age': 7}
In [2]: dict
Out[2]: {'Age': 7, 'Name': 'Zara'}
In [3]: dict2 = {'Sex': 'female' }
In [4]: dict.update(dict2)
In [5]: print "Value : %s" % dict
Value : {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}
In [6]: dict.update({'place': 'mangalore'})
In [7]: dict
Out[7]: {'Age': 7, 'Name': 'Zara', 'Sex': 'female', 'place': 'mangalore'}
In [8]: dict.update({'Place': 'mangalore'})
In [9]: dict
Out[9]:
{'Age': 7,
'Name': 'Zara',
'Place': 'mangalore',
'Sex': 'female',
'place': 'mangalore'}
In [10]: ord('p')
Out[10]: 112
In [11]: ord('A')
Out[11]: 65
In [12]: dict.update({1: 2})
In [13]: dict
Out[13]:
{1: 2,
'Age': 7,
'Name': 'Zara',
'Place': 'mangalore',
'Sex': 'female',
'place': 'mangalore'}
In [15]: ord('1')
Out[15]: 49
If you see, ord('p')
and ord('A')
, which is 112 and 65 respectively. That's probably the reason why place
came at the bottom and A came before it and '1' came at the top.
A-Z has ascii 65-90 values and a-z has 97-122.
Another Example,
In [16]: d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}
In [17]: d
Out[17]: {'ac': 33, 'ap': 102, 'bs': 10, 'gw': 20, 'za': 321}
If you want to keep track of the insertion order, then you got to use OrderedDict
In [22]: from collections import OrderedDict
In [27]: l = [('ac', 33), ('gw', 20), ('ap', 102), ('za', 321), ('bs', 10)]
In [28]: OrderedDict(l)
Out[28]: OrderedDict([('ac', 33), ('gw', 20), ('ap', 102), ('za', 321), ('bs', 10)])