[Tutor] dictionary

Sean Perry shaleh at speakeasy.net
Mon Oct 24 09:26:42 CEST 2005


Shi Mu wrote:
> I typed:
> landUse = {'res': 1, 'com': 2, 'ind': 3, "other" :[4,5,6,7]}
> and when i checked landUse, I found it become:
> {'ind': 3, 'res': 1, 'other': [4, 5, 6, 7], 'com': 2}
> why the order is changed?

the docs warn you about this.

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.

landUse = [('res', 1), ('com', 2), ('ind', 3), ("other", [4,5,6,7])]


More information about the Tutor mailing list