Iterate through Python dictionary by Keys in order -



Iterate through Python dictionary by Keys in order -

if have dictionary in python looks this:

d = {1:'a',5:'b',2:'a',7:'a'}

so values of keys irrelevant there way iterate through dictionary keys in numerical order. keys integers.

instead of saying

key in d: code....

can go through dictionary keys in order 1,2,5,7? help in advance.

additionally cannot utilize sort/sorted functions.

you can utilize this:

for key in sorted(d.iterkeys()): .. code ..

in python 3.x, utilize d.keys() (which same d.iterkeys() in python 2.x).

python dictionary order loops

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -