Python dictionary, how to keep keys/values in same order as declared? -


new python , had question dictionaries. have dictionary declared in particular order , want keep in order time. keys/values can't kept in order based on value, want in order declared it.

so if have dictionary:

d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10} 

it isn't in order if view or iterate through it, there way make sure python keep explicit order declared keys/values in?

using python 2.6

from collections import ordereddict ordereddict((word, true) word in words) 

contains

ordereddict([('he', true), ('will', true), ('be', true), ('the', true), ('winner', true)]) 

if values true (or other immutable object), can use:

ordereddict.fromkeys(words, true) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -