[melbourne-pug] dicts not always printing (and thus processing) in the same order they were created in (or, Im doing it WRONG ????)

Juan Nunez-Iglesias jni.soma at gmail.com
Fri Mar 14 00:30:23 CET 2014


Hi David,


Indeed, Python dicts place no guarantees on the order in which the items appear in iteration. If you want to preserve the insert order, try "from collections import OrderedDict". Then use OrderedDicts wherever you are using a dict now. 




Juan. 
—
Sent from Mailbox for iPhone

On Fri, Mar 14, 2014 at 10:23 AM, David Crisp <dcrisp at netspace.net.au>
wrote:

> I am having a little problems with dicts returning data not alwyas in the 
> same order depending on how long they are:
> The code reads in a line of address data from an excel spreadsheet as per 
> the following block:
> asset_field_dict[config_row[2]] = list_row[2]
> asset_field_dict[config_row[3]] = list_row[3]
> asset_field_dict[config_row[4]] = list_row[4]
> asset_field_dict[config_row[5]] = list_row[5]
> asset_field_dict[config_row[6]] = list_row[6]
> asset_field_dict[config_row[7]] = list_row[7]
> asset_field_dict[config_row[8]] = list_row[8]
> asset_field_dict[config_row[9]] = list_row[9]
> I then simply print the dict out to see what it contains:
> print(asset_field_dict)
> IF I run this with only 3 eleemnts the dict is ALWAYS in the same
> order:  (note: not correct:   it should actually be Street No, Street 
> Name, Street Type)
> {'Street Name': 'Janet', 'Street Type': 'Cres', 'Street No': 45.0}
> {'Street Name': 'Regen', 'Street Type': 'Court', 'Street No': 1.0}
> {'Street Name': 'Greenhills', 'Street Type': 'Road', 'Street No': 5.0}
> IF I run it with ALL the rows there then the data being read then the rows 
> will be randomly returned in different orders.
> For instance (sorry about the formatting)
> First time I run the code:
> {'Street No': 45.0, 'Street Type': 'Cres', 'ZipCode': 3084.0, 'Y': 
> -37.68926, 'Country': 'Australia', 'Street Name': 'Janet', 'State': 
> 'Victoria', 'City': 'Bundoora'}
> {'Street No': 1.0, 'Street Type': 'Court', 'ZipCode': 3084.0, 'Y': 
> -37.685274, 'Country': 'Australia', 'Street Name': 'Regen', 'State': 
> 'Victoria', 'City': 'Bundoora'}
> {'Street No': 5.0, 'Street Type': 'Road', 'ZipCode': 3084.0, 'Y': 
> -37.691476, 'Country': 'Australia', 'Street Name': 'Greenhills', 'State': 
> 'Victoria', 'City': 'Bundoora'}
> Second time I run the code: 
> {'Street Type': 'Cres', 'Street Name': 'Janet', 'Street No': 45.0, 
> 'ZipCode': 3084.0, 'Country': 'Australia', 'State': 'Victoria', 'Y': 
> -37.68926, 'City': 'Bundoora'}
> {'Street Type': 'Court', 'Street Name': 'Regen', 'Street No': 1.0, 
> 'ZipCode': 3084.0, 'Country': 'Australia', 'State': 'Victoria', 'Y': 
> -37.685274, 'City': 'Bundoora'}
> {'Street Type': 'Road', 'Street Name': 'Greenhills', 'Street No': 5.0, 
> 'ZipCode': 3084.0, 'Country': 'Australia', 'State': 'Victoria', 'Y': 
> -37.691476, 'City': 'Bundoora'}
> Third time i run the code:
> {'Y': -37.68926, 'City': 'Bundoora', 'State': 'Victoria', 'Country': 
> 'Australia', 'Street Type': 'Cres', 'ZipCode': 3084.0, 'Street No': 45.0, 
> 'Street Name': 'Janet'}
> {'Y': -37.685274, 'City': 'Bundoora', 'State': 'Victoria', 'Country': 
> 'Australia', 'Street Type': 'Court', 'ZipCode': 3084.0, 'Street No': 1.0, 
> 'Street Name': 'Regen'}
> {'Y': -37.691476, 'City': 'Bundoora', 'State': 'Victoria', 'Country': 
> 'Australia', 'Street Type': 'Road', 'ZipCode': 3084.0, 'Street No': 5.0, 
> 'Street Name': 'Greenhills'}
> HOW do I read the dict in the order it was written?
> If thats the wrong way of doing it,  what would be the correct way of 
> doing it?
> Regards,
> David Crisp
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> https://mail.python.org/mailman/listinfo/melbourne-pug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20140313/6a217dc3/attachment.html>


More information about the melbourne-pug mailing list