Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 534
helpermethod Avatar asked Dec 29 '10 00:12

helpermethod


People also ask

How do you iterate through a dictionary order?

Another simple solution to iterate over a dictionary in the sorted order of keys is to use the dict. keys() with the sorted() function. Alternatively, to iterate in reverse order of keys, you can specify the reverse argument of the sorted() function as True .

How do you iterate keys and values in Python?

In Python, to iterate the dictionary ( dict ) with a for loop, use keys() , values() , items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() . Use the following dictionary as an example. You can iterate keys by using the dictionary object directly in a for loop.

How do you iterate over a dictionary key?

In order to iterate over the values of the dictionary, you simply need to call values() method that returns a new view containing dictionary's values.


2 Answers

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).

like image 145
Mark Byers Avatar answered Sep 22 '22 12:09

Mark Byers


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] 
like image 45
Lennart Regebro Avatar answered Sep 22 '22 12:09

Lennart Regebro