how to keep order key in a dictionary

Bruno Desthuilliers bdesth.quelquechose at
Sun Nov 4 17:41:37 CET 2007

azrael a écrit :
> I 'm currenty working on a project for which it would be great to use
> a dictionary. At the begining I have a list of strings that should
> represent the keys in the dictionary. When I try to create a
> dictionary it rearanges the keys. For this dictionary it is realy
> important to keep the right order. Is it possible to arange them in a
> specific order?
No (not with the builtin dict type at least). You have to keep the order 
by yourself. The nice thing is that you already have this : your list of 
strings. Then you just have to iterate over this list and get to the dict:

for key in list_of_strings:
   print dic[key]


More information about the Python-list mailing list