title: loop through array -- python
body: <p>I am currently writing a program that will take in a text file, and then count the frequency of each word in the file, after lower casing every word and stripping its punctuation. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>import sys 
  import string
  
  incoming =[]
  freq =[]
  word =[]
  count = 0
  index = 0
  i = 0
  
  with open(sys.argv[1], "r") as word_list:
      for line in word_list:
          #word is the string of the .txt file
  
          #strips punctuation and lower cases each word
          for words in line.split():
              words = words.translate(string.maketrans("",""), string.punctuation)
              words = words.lower()
              incoming.append(words)
          #incoming is now an array with each element as a word from the file     
  
      for i in range(len(incoming)-1):
          if (incoming[i]) not in word:
              #WORD[i] = word[index]
              word[index] = incoming[i]
              freq[index] = 1
              index += 1
  
          else: 
              freq[index] = freq[index] + 1
  
  
      for j in word:
          print "%s %d", word[j], freq[j]
  </code></pre>
  
  <p>I am getting the error: </p>
  
  <pre><code>  File "wordfreq.py", line 26, in &lt;module&gt;
      word[index] = incoming[i]
  IndexError: list assignment index out of range
  </code></pre>
  
  <p>But I fail to see how it can be out of range. Neither <code>index</code> nor <code>i</code> go out of range as far as I can tell. I am new to Python and am having a lot of trouble with the 'for' loop syntax. Any tips would be much appreciated. </p>