>>> basket = {'fruit' : 'apple','veg' : 'potato', 'grain' : 'rice', 'liquor' : 'wine'}
>>> for k,v in basket.iteritems():
... print k,v
...
veg potato
liquor wine
fruit apple
grain rice
>>>
As can be seen from the above example, the key-value pair are not printed from left to right or from right to left.
To get a sorted result, I used
>>> basket = {'fruit' : 'apple','veg' : 'potato', 'grain' : 'rice', 'liquor' : 'wine'}
>>> for k,v in sorted(basket.iteritems()):
... print k,v
...
fruit apple
grain rice
liquor wine
veg potato
>>>
When I try to get the indices of the key-value pairs, I get
>>> basket = {'fruit' : 'apple','veg' : 'potato', 'grain' : 'rice', 'liquor' : 'wine'}
>>> for k,v in enumerate(basket.iteritems()):
... print k,v
...
0 ('veg', 'potato')
1 ('liquor', 'wine')
2 ('fruit', 'apple')
3 ('grain', 'rice')
>>>
Why does this happen? How can I print the key-value pairs as it appears in the dictionary definition, from left to right?