- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the first repeated word in a string in Python using Dictionary
In a given sentence there may be a word which get repeated before the sentence ends. In this python program, we are going to catch such word which is repeated in sentence. Below is the logical steps we are going to follow to get this result.
- Splits the given string into words separated by space.
- Then we convert these words into Dictionary using collections
- Traverse this list of words and check which first word has the frequency > 1
Program - Find the Repeated Word
In the below program we use the counter method from the collections package to keep a count of the words.
from collections import Counter def Repeat_word(load): word = load.split(' ') dict = Counter(word) for value in word: if dict[value]>1: print (value) return if __name__ == "__main__": input = 'In good time in bad time friends are friends' Repeat_word(input)
Running the above code gives us the following result −
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in Java
- Find the first repeated word in a string in C++
- Find the first repeated character in a string using C++.
- Find repeated character present first in a string in C++
- Word Dictionary using Python Tkinter
- Find the second most repeated word in a sequence in Java
- Find the first maximum length even word from a string in C++
- Second most repeated word in a sequence in Python?
- Longest Word in Dictionary in Python
- Python - Find the length of the last word in a string
- Print first letter of each word in a string using C# regex
- Find frequency of each word in a string in Python
- Getting first letter of each word in a String using regex in Java