Featured Post

Linux daemon using Python daemon with PID file and logging

The python-daemon package ( PyPI listing , Pagure repo ) is very useful. However, I feel it has suffered a bit from sparse documentation, an...

2013-04-05

Small Python tip - sorted iterating over dictionary

Python dictionaries are great. However, iterating over dictionaries results in an unsorted order:
In [1]: d = {'a':10, 'b': 20, 'c': 30}

In [2]: for key,value in d.iteritems():
   ...:     print key, value
   ...:
a 10
c 30
b 20
The fix is to use sorted():

In [3]: for key,value in sorted(d.iteritems()):
   ...:     print value
   ...:
a 10
b 20
c 30