Q:

counting word frequencies in a corpus is taking too long

My task is to:
Print out a list of all the words that occur in the corpus and their frequencies. This list should be sorted by word frequencies in descending order (most frequent word first)."
def printWordFrequencies(index, vocab):
    #print("Your task 3: print out a list of all the words that occur in the corpus and their frequencies. This list should be sorted by word frequencies in descending order (most frequent word first).")
    newList = []
    for i in index:
        newList.append([i, len(index[i])])
        sorted(newList)
    return newList

index is a dictionary (associative array) that maps words to a list of their positions and vocab is an alphabetically sorted list of the vocabulary used in the corpus.
The function that I have written has two problems. Firstly, it does not sort properly. It gives the right word frequency like [.., ['plot', 128], ['two', 166] , ..] but it does not sort depending on its frequency.
Secondly, it takes way too long time to run. I am assuming that it is because it's trying to append and sort such a long list, but I am not sure how to fix this issue.
One small thing is that, I am not sure why I need vocab as an input parameter.

A:

Firstly, you would only need to sort newList once, at the end. Currently you are sorting it for each iteration of your loop which is why it is running so slowly. Using sorted() also does not change your original list, but returns a new sorted list, so in your code, the sorting has no effect as you are not storing the list returned.
Secondly, to sort based on the second item in each entry, you can give sorted() a key to use. In this case, it sorts first based on the second length item x[1], and if two entries have the same length, it sorts based on the first entry x[0].
def printWordFrequencies(index, vocab):
    newList = []

    for i in index:
        newList.append([i, len(index[i])])

    return sorted(newList, key=lambda x: [x[1], x[0]], reverse=True)

This could be further simplified by using a list comprehension:
def printWordFrequencies(index, vocab):    
    return sorted([i, len(index[i])]] for i in index, key=lambda x: [x[1], x[0]], reverse=True)

