[Tutor] dictionaries

alan.gauld@bt.com alan.gauld@bt.com
Thu, 21 Feb 2002 12:11:44 -0000

> I would like to know why the C part of my dictionary when I 
> update it from the 2nd one goes before B.
>>> x = "C"
>>> y = "3"
>>> dict2 = {x:y}
>>> dict.update(dict2)
>>> dict
> {'A': '1', 'C': '3', 'B': '2'}

Dictionaries do not guarantee order. Its part of the characteristics 
of a dictionary(or hash table). Thats why if you want to process 
a dictionary in order you have to get the keys into a list and 
then sort them:

for key in mydict.keys().sort():
    # process mydict[key]

If you want the full explanation look up a text book on how 
hash tables work!

Alan g