Dictionary order


In Python when we insert key/value pair to the dictionary, how it is stored ? Is it stores randomly or any order?


its random, depending in how much depth you want to know:


however, for pyton3.6+ are ordered:

if you want to implement ordered dictionary in earlier version of python, this is possible:



It should never be assumed that dictionaries are ordered. Period.

If your want an ordered dictionary. then order one. Don’t expect the environment to order one up for you.


If you want to process the items in a dictionary in a particular order, you can use a list or other object to manage the keys. For example, if you want to output the keys in alphabetical order, this will do it …

key_list = prices.keys()

for key in key_list:
  print key
  print "price: %d" % prices[key]
  print "stock: %d" % stock[key]


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.