Issue in printing top 20 dictionary items by dictionary value

Larry Hudson orgnut at
Sat Oct 4 23:44:20 CEST 2014

On 10/04/2014 10:36 AM, Shiva wrote:
> What I don't understand is:
>      for w in eachword:
>          textstorage[w]=textstorage.get(w, 0) + 1
> How does textstorage.get(w,0)+1 give the count of the word??

Very long-winded explanation:  (But to shorten it a bit, I'm going to use ts in place of 
textstorage.  Also lhs = left-hand-side and rhs = right-hand-side.)

What we're trying to do here is to update the word count.  We could (erroneously) write it as:

ts[w] = ts[w] + 1

If w already exists in the ts dictionary, this works fine.  But if it does not it will abort 
with a KeyError when it comes to the ts[w] on the rhs of the assignment.

The get() method is an alternate way of accessing the value of a key in a dictionary, but with a 
default value given as well.  Now let's break down the statement

ts[w] = ts.get(w, 0) + 1

Case 1:  w already exists in the ts dictionary:

ts.get(w, 0) gets the value of ts[w] (the current word count), adds 1, which is then used to 
update the word-count value of ts[w] (on the lhs of the assignment).

case2:  w does not exist in the ts dictionary:

ts.get(w, 0) gives the default value of 0, and 1 is added to that.  ts[w] on the lhs of the 
assignment does not exist, so a new entry is created in the ts dictionary with the given w as 
the key, and the value is initialized with the 1 from the get()+1.

Make sense?

      -=- Larry -=-

More information about the Python-list mailing list