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

Brett Wilkins brett at brett.geek.nz
Fri Mar 14 00:30:13 CET 2014


Hi David,

Python Dictionaries are unordered by definition. If you want an ordered dict, use an OrderedDict: http://docs.python.org/2/library/collections.html#collections.OrderedDict
-- 
Brett Wilkins

On 14 March 2014 at 10:23:59 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/20140314/aeb95c73/attachment-0001.html>


More information about the melbourne-pug mailing list