I'm writing a program in Python 3 and part of it's functionality is to find out what word occurs the most in list and to return the number of occurrences of that word. I have code that works, but part of the requirement is that it take a list of 200,000+ words and complete this activity in under a few seconds, and my code takes a really long time to run. I was wondering what suggestions you may have for speed improvements in this method.
def max_word_frequency(words):
"""A method that takes a list and finds the word with the most
occurrences and returns the number of occurences of that word
as an integer.
"""
max_count = 0
for word in set(words):
count = words.count(word)
if count > max_count:
max_count = count
return max_count
I have contemplated using a dictionary as they are hashable and super speedy compared to lists but I cannot quite figure out how to implement this yet.
Thank you for your time everyone!
- Finn