I have a list of words:
words = ['all', 'awesome', 'all', 'yeah', 'bye', 'all', 'yeah']
And I want to get a list of tuples:
[(3, 'all'), (2, 'yeah'), (1, 'bye'), (1, 'awesome')]
where each tuple is...
(number_of_occurrences, word)
The list should be sorted by the number of occurrences.
What I've done so far:
def popularWords(words):
dic = {}
for word in words:
dic.setdefault(word, 0)
dic[word] += 1
wordsList = [(dic.get(w), w) for w in dic]
wordsList.sort(reverse = True)
return wordsList
The question is...
Is it Pythonic, elegant and efficient? Are you able to do it better? Thanks in advance.