Jake Blank wrote: > I finally got it. > This was the code: > for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): > print (k, word_count[k]) > > The only question i have now is how to limit the amount of returns the > program runs to the first 15 results.
Hint: you can make a list of the first N items of a list with some_list = some_list[:N] The list in question is sorted(word_count, key=lambda x:word_count[x], reverse=True) You might also try the heapq.nlargest() function: >>> word_count {'pewter': 12, 'rawboned': 2, 'accolade': 2, 'Messiah': 15, 'summoning': 17, 'sleeking': 6, 'parse': 14, 'Freya': 8, 'chroniclers': 13, 'faith': 1} >>> import heapq >>> for word in heapq.nlargest(5, word_count, key=lambda w: word_count[w]): ... print(word, word_count[word]) ... summoning 17 Messiah 15 parse 14 chroniclers 13 pewter 12 See also <https://docs.python.org/dev/library/heapq.html#heapq.nlargest> _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor