data collections our last step is to print report that summarizes the contents of counts one approach might be to print out the list of words and their associated counts in alphabetical order here' how that could be doneget list of words that appear in document uniquewords list(counts keys()put list of words in alphabetical order uniquewords sort(print words and associated counts for in uniquewordsprint(wcounts[ ]for large documenthoweverthis is unlikely to be useful there will be far too many wordsmost of which only appear few times more interesting analysis is to print out the counts for the most frequent words in the document in order to do thatwe will need to create list that is sorted by counts (most to fewestand then select the first items in the list we can start by getting list of key-value pairs using the items method for dictionaries items list(counts items()here items will be list of tuples ( [('foo', )('bar', )('spam', )]if we simply sort this list (items sort()python will put them in standard order unfortunatelywhen python compares tuplesit orders them by componentsleft to right since the first component of each pair is the worditems sort(will put this list in alphabetical orderwhich is not what we want to sort our list of items according to frequencywe can use the key-function trick again this timeour function will take pair as parameter and return the second item in the pair def byfreq(pair)return pair[ notice that tupleslike listsare indexed starting at so returning pair[ hands back the frequency part of the tuple with this comparison functionit is now simple matter to sort our items by frequency items sort(key=byfreqbut we're not quite finished yet when we have multiple words with the same frequencyit would be nice if those words appeared in the list in alphabetical order within their frequency group that iswe want the list of pairs primarily sorted by frequencybut sorted alphabetically within each level how can we handle this double-sortednessif you look at the documentation for the python sort method (via help([sort]you'll see that this method performs "stable sort *in placeas you can probably inferbeing "in place