means that the method modifies the list that it is applied to rather than producing newsortedversion of the list but the critical point for us here is the word "stable sorting algorithm is stable if equivalent items (items that have equal keysstay in the same relative position to each other in the resulting list as they were in the original since the python sorting algorithm is stableif all the words were in alphabetical order before sorting them by frequencythen words having the same frequency will still be in alphabetical order to get the result we wantwe just need to sort the list twicefirst by words and then by frequency items sort(items sort(key=byfreqreverse=trueorders pairs alphabetically orders by frequency have added one last wrinkle here supplying the keyword parameter reverse and setting it to true tells python to sort the list in reverse order the resulting list will go from highest frequency to lowest now that our items are sorted in order from most to least frequentwe are ready to print report of the most frequent words here' loop that does the trickfor in range( )wordcount items[iprint("{ : }format(wordcountthe loop index is used to get the next next pair from the list of itemsand that item is unpacked into its word and count components the word is then printed left-justified in fifteen spacesfollowed by the count right-justified in five spaces that about does it here is the complete programwordfreq py def byfreq(pair)return pair[ def main()print("this program analyzes word frequency in file"print("and prints report on the most frequent words \ "get the sequence of words from the file fname input("file to analyze"text open(fname,' 'read(text text lower(for ch in '!"#$%&()*+,/:;?@[\\]^ '{|}~'text text replace(ch' an experienced python programmer would probably write this loop body as single line using the tuple unpacking operator *print(" : format(*items[ ]the curious should consult the python documentation to learn more about this handy operator