Python program for most frequent word in Strings List - GeeksforGeeks

04 Oct, 2021
Given Strings List, write a Python program to get word with most number of occurrences.
Example:
Input : test_list = [“gfg is best for geeks”, “geeks love gfg”, “gfg is best”] Output : gfg Explanation : gfg occurs 3 times, most in strings in total.
Input : test_list = [“geeks love gfg”, “geeks are best”] Output : geeks Explanation : geeks occurs 2 times, most in strings in total. 
Method #1 : Using loop + max() + split() + defaultdict()
In this, we perform task of getting each word using split(), and increase its frequency by memorizing it using defaultdict(). At last, max(), is used with parameter to get count of maximum frequency string.
Python3
# Python3 code to demonstrate working of# Most frequent word in Strings List# Using loop + max() + split() + defaultdict()from collections import defaultdict # initializing Matrixtest_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"] # printing original listprint("The original list is : " + str(test_list)) temp = defaultdict(int) # memoizing countfor sub in test_list:    for wrd in sub.split():        temp[wrd] += 1 # getting max frequencyres = max(temp, key=temp.get) # printing resultprint("Word with maximum frequency : " + str(res))
Output:
The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg
Method #2 : Using list comprehension + mode()
In this, we get all the words using list comprehension and get maximum frequency using mode().
Python3
# Python3 code to demonstrate working of# Most frequent word in Strings List# Using list comprehension + mode()from statistics import mode # initializing Matrixtest_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"] # printing original listprint("The original list is : " + str(test_list)) # getting all wordstemp = [wrd for sub in test_list for wrd in sub.split()] # getting frequencyres = mode(temp) # printing resultprint("Word with maximum frequency : " + str(res))
Output:
The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg
Append all words to empty list and calculate frequency of all words using Counter() function.
Find max count and print that key.
Below is the implementation:
Python3
# Python3 code to demonstrate working of# Most frequent word in Strings List from collections import Counter # function which returns# most frequent worddef mostFrequentWord(words):       # Taking empty list    lis = []    for i in words:               # Getting all words        for j in i.split():            lis.append(j)                 # Calculating frequency of all words    freq = Counter(lis)         # find max count and print that key    max = 0    for i in freq:        if(freq[i] > max):            max = freq[i]            word = i            return word  # Driver code# initializing strings listwords = ["gfg is best for geeks", "geeks love gfg", "gfg is best"] # printing original listprint("The original list is : " + str(words)) # passing this words to mostFrequencyWord function# printing resultprint("Word with maximum frequency : " + mostFrequentWord(words))# This code is contributed by vikkycirus
Output:
The original list is : ['gfg is best for geeks', 'geeks love gfg', 'gfg is best']
Word with maximum frequency : gfg
vikkycirus
simranarora5sos
sooda367
simmytarika5
Python list-programs
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python program to check whether a number is Prime or not
Python | Convert a list to dictionary