title: List index out of range - Index Error Python
body: <p>I am writing a function that will iterate through a list of text items -  parse each item, and append the parsed items back into a list. 
  The code is as below: </p>
  
  <pre><code>clean_list = []
  
  def to_words( list ):
      i = 0
      while i &lt;= len(list):
          doc = list[i]
          # 1. Remove HTML
          doc_text = BeautifulSoup(doc).get_text() 
          # 2. Remove non-letters (not sure if this is advisable for all documents)       
          letters_only = re.sub("[^a-zA-Z]", " ", doc_text) 
          # 3. Convert to lower case, split into individual words
          words = letters_only.lower().split()                                               
          # 4. Remove stop words
          stops = set(stopwords.words("english"))
          meaningful_words = [w for w in words if not w in stops]   
          # 5. Join the words back into one string separated by space, and return the result.
          clean_doc = ( " ".join( meaningful_words ))   
          i = i+1
          clean_list.append(clean_doc)
  </code></pre>
  
  <p>But when I pass the list into this function, <code>to_words(list)</code>, I get this error: <code>IndexError: list index out of range</code></p>
  
  <p>I tried experimenting without technically defining the <code>to_words</code> function i.e. avoiding the loop, manually changing i as 0,1,2 etc, and following through the steps of the function; this works fine. </p>
  
  <p>Why am I facing this error when I use the function (and loop)? </p>