There’s an existing function that ends in the following, where d is a dictionary:
return d.iteritems()
that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items sorted by key. How do I do that?
Haven’t tested this very extensively, but works in Python 2.5.2.
If you are used to doing
for key, value in d.iteritems(): ...instead of iterators, this will still work with the solution aboveWith Python 3.x, use
d.items()instead ofd.iteritems()to return an iterator.