Removing all duplicate word from a given sentence in Python

Python Methods and Functions

A proposal is given. Remove all duplicate words from a given Python sentence.






Example

 Input: I am a peaceful soul and blissful soul. Output: I am a peaceful soul and blissful. 



Algorithm

 Step 1: Split input sentences separated by space into words. Step 2: So to get all those strings together first we will join each string in a given list of strings. Step 3: now create a dictionary using the counter method which will have strings as key and their Frequencies as value. Step 4: Join each words are unique to form single string. 



Sample code

 f rom collections import Counter def remov_duplicates (st): st = st.split ("") for i in range (0, len (st)): st [i] = "" .join (st [i]) dupli = Counter (st) s = "" .join (dupli.keys ()) print (" After removing the sentence is :: & gt; ", s) # Driver program if __name__ == " __main__ ": st = input (" Enter the sentence ") remov_duplicates (st) 



Output

 Enter the sentence :: & gt; i am a peaceful soul and blissful soul After removing the sentence is :: & gt; i am a peaceful soul and blissful 







Get Solution for free from DataCamp guru