Beginning word counting program only produces output for the last line in python
Question: I am a beginner programmer attempting to build a simple program. It should
count every word in the file but as I wrote, it only counts the last line of
text.

    
    
    tm = open('myfile.txt', 'r')
    for line in tm:
        line = line.replace ('\n', '')
        line = line.strip()
        line = line.translate(None, '!#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
        line = line.lower()
        line = line.split(' ')
        list = line
    dict ={}
    for word in list:
        dict[word]=1
    if word in dict:
        count = dict[word]
        count += 1
        dict[word] = count
    else:
        dict[word]=1
    for word,count in dict.iteritems():
        print word + ": " + str(count)
    

My output is this

    
    
    about: 1
    to: 1
    subscribe: 1
    hear: 1
    new: 1
    our: 1
    newsletter: 1
    email: 1
    ebooks: 2
    

for a 500 page document any help is appreciated


Answer: Replace this line in your code:

    
    
    list = line # that's not how you add elements to a list!
    

With this other:

    
    
    list.extend(line)
    

And it'd be a good idea to rename to `lst` the `list` variable, because `list`
is a built-in and it's a bad idea to overwrite it. Same thing for `dict`, you
should not use that as a variable name.

Another good idea: use a
[`Counter`](http://docs.python.org/2/library/collections.html#collections.Counter)
object to keep track of the word frequency, it's much easier than updating the
dictionary's counter values by hand. The whole block of code where you create
and fill the dictionary can be replaced by this:

    
    
    from collections import Counter
    d = Counter(lst) # notice the suggested variable names
    

