Most used Word in a Sentence – Python Ways

How to find out the most used word in a sentence. This is very useful when you want to analyse a sentence or a text file containing English words.  So the problem is

sentance : "this is me and that is my first attempt in python programming"
output   :  is

To solve this problem the approach is something like this

  1. change the whole sentence into a list
  2. scan each word from this list
  3. find out the frequency of this word
  4. check if the frequency of this word is already present in the dictionary or not
  5. if the frequency already exist then add the word in the same freqency
  6. else, add this frequency as a new key and add the word as its value

The solution is now implemented in a python program

str1='this is me and this is in python for class 12 students'
list1 = str1.split()
for x in list1:
    n = list1.count(x)
    if not n in freq:
        freq[n] = [x] 
        if not x in freq[n]:
            freq[n] = freq[n]+[x]

The output of the above code is

rakesh@rakesh:~$ /usr/bin/python3 /home/rakesh/
[(2, ['this', 'is'])]
Print Friendly, PDF & Email

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to See your article appearing on the main page and help other students/teachers.