[Tutor] dictionary

Alan Gauld alan.gauld at freenet.co.uk
Mon Oct 24 21:30:43 CEST 2005


> A dictionary stores its values based on the keys. Internally Python 
> sorts the keys to make the lookup meet performance expectations.
> 
> You can not expect a dictionary to return keys in the other you added 
> them. If you want that, store the items as tuples in a list.

Or you can sort the keys and iterate over that:

keys = dic.keys()
keys.sort()  # sorts in place!
for key in keys.sort(): print key,dic[key]

Alan G


More information about the Tutor mailing list