You can use this:
for key in sorted(D.iterkeys()):
.. code ..
In Python 3.x, use D.keys() (which is the same as D.iterkeys() in Python 2.x).
You can use this:
for key in sorted(D.iterkeys()):
.. code ..
In Python 3.x, use D.keys() (which is the same as D.iterkeys() in Python 2.x).