Iterating over key/value pairs in a dict sorted by keys

In Python 2.x both will give you the same result. The difference between them is that items constructs a list containing the entire contents of the dictionary whereas iteritems gives you an iterator that fetches the items one at a time. In general iteritems is a better choice because it doesn't require so much memory. But here you are sorting the result so it probably won't make any significant difference in this situation. If you are in doubt iteritems is a safe bet. If performance really matters then measure both and see which is faster.

In Python 3.x iteritems has been removed and items now does what iteritems used to do, solving the problem of programmers wasting their time worrying about which is better. :)

As a side note: if you are counting occurrences of words you may want to consider using collections.Counter instead of a plain dict (requires Python 2.7 or newer).


As per Marks answer: In Python 2, use iteritems(), in Python 3 use items().

And additionally; If you need to support both (and don't use 2to3) use:

counts = count_words(filename)
for word in sorted(counts):
     count = counts[word]