forcing dictionary order

Bob Kline bkline at rksystems.com
Thu Mar 29 14:46:02 EST 2001


On Thu, 29 Mar 2001, Bob van der Poel wrote:

> 
> I'd like (not a must!) to walk a dictionary in the same order in
> which it was created. Example:
> 
> 	foo={ key1: (), key2: (), key3: () }
> 
> If I do:
> 	
> 	for a in foo.keys(): ...
> 
> the order is not "key1, key2, key3", which is what I want. Note, I
> can't do something simple like sort() on the keys since the are not
> in any alphanumeric order. I'm just using this to generate a largish
> table for another program, and the actual order doesn't matter for
> it to work properly, but it would nicer if it were in the correct
> logical order. So, I'm not going to spend a lot of time on this, but
> if there is a simple way????

Sure.  Maintain a parallel array of references to the entries in the
dictionary as the dictionary is populated.

-- 
Bob Kline
mailto:bkline at rksystems.com
http://www.rksystems.com





More information about the Python-list mailing list