title: Sorting averages by highest to lowest in Python from text files
body: <p>I am trying to sort averages of scores in a class by pupil, from highest to lowest. </p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>with open("class.txt") as f:
      d = {}
  
      for line in f:
          column = line.split(":")
          names = column[0]
          scores = int(column[1].strip())
  
          count = 0
          while count &lt; 3:
              d.setdefault(names, []).append(scores)
              count = count + 1
      for names, v in sorted(d.items()):
          average = (sum(v)/len(v))
          print(names,average)
      averages=[]
      averages.append(average)
  </code></pre>
  
  <p>I have worked out the averages however, I'm stuck on how I could sort these averages by highest to lowest, here is what ive tried</p>
  
  <pre><code>list = sorted(averages, key=lambda tup: tup[1], reverse=True)
  </code></pre>
  
  <p>However, it gives the error..</p>
  
  <pre><code>TypeError: 'float' object is not subscriptable
  </code></pre>
  
  <p>I also noticed that it doesn't work the averages of the last three scores only, it works out the average for all the scores of the student in the text file, however I would like it to work out only the three recent scores of each student</p>
  
  <p>I'm quite new to Python and I am not sure what I am doing wrong, so any pointers of help would be much appreciated, Thank you in advance!</p>