This tutorial we will find out the way to remove duplicated words from given sentence in Python. We will check given string if there are duplicated word then we will remove from the string and return new string after we filter all duplicated word out.
Look at example how input/output should display
Input:
Python is and Java is also great
Output:
and great Java Python is also
The below method will help you to approch result.
Algorithm
Syntax:
text = "cambotutorial is free tutorial and website and free tools"
# split and return list of words
lst = text.split()
newlist=[]
for i in lst:
# text.count(i) return number of duplicate in sentence
if (text.count(i)>=1 and (i not in newlist)):
# add into new list
newlist.append(i)
#concatenation list key with space
concate = ' '.join(newlist)
print(concate)
Output:
cambotutorial is free tutorial and website tools
Set()
is built-in data type in Python that can hold many items with it like an array. Set not allow to stroe duplicated values which mean automatically filter repeated items. Following this concept by take advantage this data type we can remove duplicated item in list as well.
Algorithm
Syntax:
text = "cambotutorial is free tutorial and website and free tools"
# Split and return list of words
lst = text.split(" ")
# Pass to set datatype
no_dup = set(lst)
#concatenation list key with space
result = " ".join(no_dup)
print(result)
Output:
cambotutorial is free tutorial and website tools
fromkeys()
MethodThis built-in is quit similar to Set()
datatype because dictionary in Python only have unique key. This means the keys never contain duplicated values.
Algorithm
fromkeys()
method, convert list to dictionary keySyntax:
text = "cambotutorial is free tutorial and website and free tools"
# split and return list of words
lst = text.split()
#filter get only keys
key = dict.fromkeys(lst)
#concatenation list key with space
result = " ".join(key)
print(result)
Output:
cambotutorial is free tutorial and website tools
Counter()
MethodAnother way to filter out duplicated method by using Counter() which is similar to 3rd method. Counter() will convert list into object key and value pair that we can use fromkeys() to remove duplicated items.
Algorithm
Counter()
fromkeys()
method.Syntax:
# import Counter from collections package
from collections import Counter
text = "cambotutorial is free tutorial and website and free tools"
# split and return list of words
lst = text.split(" ")
# return keys in words and value as number of word duplicated
word_dic = Counter(lst)
key = word_dic.keys()
#concatenation list key with space
result = " ".join(key)
print(result)
Output:
cambotutorial is free tutorial and website tools
What do you think which one is the best method for you to remove duplicated words?
Python
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him