Write a Python program to remove duplicate words from a given string


The program takes a string as input and removes any duplicate words from it. The program then prints the original string followed by the string with duplicates removed to the console. The program first initializes a string variable str with a space-separated list of words.

The program then prints the original string to the console using the print() function with a newline character \n and a tab character \t added for formatting. The program then splits the input string into a list of words using the split() function and stores it in a variable l. Next, the program initializes an empty list temp that will be used to store unique words.

The program then loops through each word in the list l, and if the word is not already in the temp list, it appends the word to the temp list. Finally, the program prints the string with duplicates removed to the console using the join() function to concatenate the words in the temp list into a single string with a space separator.

Source Code

str = "Python Exercises Practice Solution Exercises"
print("Before Removing Duplicates :\n\t",str)
l = str.split()
temp = []
for x in l:
	if x not in temp:
		temp.append(x)
print("After Removing Duplicates :\n\t",' '.join(temp))

Output

Before Removing Duplicates :
         Python Exercises Practice Solution Exercises
After Removing Duplicates :
         Python Exercises Practice Solution


Example Programs