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
Post a Comment