When it is required to find the most frequent word in a list of strings, the list is iterated over and the ‘max’ method is used to get the count of the highest string.
Below is a demonstration of the same
from collections import defaultdict my_list = ["python is best for coders", "python is fun", "python is easy to learn"] print("The list is :") print(my_list) my_temp = defaultdict(int) for sub in my_list: for word in sub.split(): my_temp[word] += 1 result = max(my_temp, key=my_temp.get) print("The word that has the maximum frequency :") print(result)
The list is : ['python is best for coders', 'python is fun', 'python is easy to learn'] The word that has the maximum frequency : python
The required packages are imported into the environment.
A list of strings is defined and is displayed on the console.
A dictionary of integers is created and assigned to a variable.
The list of strings is iterated over and split based on spaces.
The count of every word is determined.
The maximum of these values is determined using the ‘max’ method.
This is assigned to a variable.
This is displayed as output on the console.