[Tutor] Dictionary Issue

Alan Gauld alan.gauld at btinternet.com
Sat Aug 8 20:38:35 CEST 2015

On 08/08/15 00:05, Ltc Hotspot wrote:
> Hi Alan,
> On line 15, I replaced: 'count[address] = count.get(address, 0) + 1' 
> with 'line = Counter(address)'.

line = Counter(address)

will create a new Counter object with the address in it with a count 
value of 1.
Every line in the file will create a new Counter, overwriting the 
previous one.
At the end of the loop you will have exactly 1 Counter containing the 
last item
in the loop and a count of 1.

Instead create the Counter before the loop and then use the update()
method to add the address to it. Recall we talked about reading the Counter
documentation? It describes the update() method.

One you have the Counter with all the addresses in you can get the max 
and key directly without using a second loop and your maxkee and maxval

Again read the Counter documentation to see how.

Finally remember the imports. You never show us those but the Counter will
not work unless you import it.

> Question: What is the cause of the empty dict?

You create it but never use it. You replaced the line where count got
populated with your Counter call. So count never gets populated.
Just because Counter sounds like count does not mean they are
in any way connected.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list