Iterating over key/value pairs in a dict sorted by keys
- by Helper Method
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()):
print word, count
However, calling iteritems() instead of items() produces the same output
for word, count in sorted(count_words(filename).iteritems()):
print word, count
Now, which one should I choose in this situation? I consulted the Python tutorial but it doesn't really answer my question.