I have the following code, which just print the key/value pairs in a dict (the pairs are sorted by keys):
for word, count in sorted(count_words(filename).items()
As per Marks answer: In Python 2, use iteritems(), in Python 3 use items().
iteritems()
items()
And additionally; If you need to support both (and don't use 2to3) use:
2to3
counts = count_words(filename) for word in sorted(counts): count = counts[word]