Iterating over key/value pairs in a dict sorted by keys
Posted
by
Helper Method
on Stack Overflow
See other posts from Stack Overflow
or by Helper Method
Published on 2010-12-29T00:32:21Z
Indexed on
2010/12/29
0:54 UTC
Read the original article
Hit count: 116
python
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.
© Stack Overflow or respective owner