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