title: IndexError: list index out of range when using tuples
body: <p>I'm very confused. I get an error on line 43 saying that the list index is out of range. Any help is appreciated.</p>
  <pre><code>def tokenize(lines):
  words = []
  for line in lines:
      start = 0
      end = start + 1
      while start &lt; len(line):
          character = line[start]
          if character.isspace():
              end += 1
          elif character.isalpha():
              end = start + 1
              while end &lt; len(line) and line[end].isalpha():
                  end += 1
              words.append(line[start:end].lower())
          elif character.isdigit():
              end = start + 1
              while end &lt; len(line) and line[end].isdigit():
                  end += 1
              words.append(line[start:end])
          else:
              end += 1
              words.append(line[start:end])
          start = end
  return words
  
  
  def countWords(words, stopWords):
      wordDict = {}
      for word in words:
          if word in stopWords:
              continue
          elif not word in wordDict:
              wordDict[word] = 1
          else:
              frequency = wordDict.get(word)
              wordDict[word] = frequency + 1
      return wordDict
  
  
  def printTopMost(frequencies, n):
      listOfTuples = sorted(frequencies.items(), key=lambda x:x[1], reverse=True)
      for x in range(n):
          pair = listOfTuples[x]
          word = pair[0]
          frequency = str(pair[1])
          print(word.ljust(20), frequency.rjust(5))
  </code></pre>
  <p><code>pair = listOfTuples[x]</code> gives me an error. Please help me why do i have to add this much text it says mostly code please.</p>
  <p>This is how the function is called: (test.py) there are instructions for the other functions I have created like tokenize and countWords also, but the error I'm getting is not a part of that which is why i've left those out.</p>
  <pre><code>def printTopMost(freq,n):
      saved = sys.stdout
      sys.stdout = io.StringIO()
      wordfreq.printTopMost(freq,n)
      out = sys.stdout.getvalue()
      sys.stdout = saved
      return out
  
  
      test(printTopMost,({&quot;horror&quot;: 5, &quot;happiness&quot;: 15},0),&quot;&quot;)
      test(printTopMost,({&quot;C&quot;: 3, &quot;python&quot;: 5, &quot;haskell&quot;: 2, &quot;java&quot;: 
      1},3),&quot;python                  5\nC                       3\nhaskell                 
      2\n&quot;)
  </code></pre>
  <p>Full error message</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:/Users/Daniel/Documents/Scripts/Chalmers/lab1/test.py&quot;, line 81, in &lt;module&gt;
      run()
    File &quot;C:/Users/Daniel/Documents/Scripts/Chalmers/lab1/test.py&quot;, line 70, in run
      test(printTopMost,({},10),&quot;&quot;)
    File &quot;C:/Users/Daniel/Documents/Scripts/Chalmers/lab1/test.py&quot;, line 8, in test
      z = fun(*x)
    File &quot;C:/Users/Daniel/Documents/Scripts/Chalmers/lab1/test.py&quot;, line 41, in printTopMost
      wordfreq.printTopMost(freq,n)
    File &quot;C:\Users\Daniel\Documents\Scripts\Chalmers\lab1\wordfreq.py&quot;, line 4, in printTopMost
      pair = listOfTuples[x]
  IndexError: list index out of range
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>Condition failed:
     printTopMost({'horror': 5, 'happiness': 15}, 0) == ''
  printTopMost returned/printed:
  happiness               15
  horror                   5
  
  Condition failed:
     printTopMost({'C': 3, 'python': 5, 'haskell': 2, 'java': 1}, 3) == 'python                  5\nC                       3\nhaskell                 2\n'
  printTopMost returned/printed:
  python                   5
  C                        3
  haskell                  2
  java                     1
  </code></pre>
  <p><a href="https://i.imgur.com/9SciXtx.png" rel="nofollow noreferrer">https://i.imgur.com/9SciXtx.png</a></p>