How to Remove all Duplicates Words in Python

In this tutorial, we will program 'How to Remove All Duplicate Words in Python.' We will learn how to remove all possible duplicates of words from a sentence. The objective is to safely remove only the words that are duplicates. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you can do it yourself with ease. The program I will show you covers the basics of programming to remove possible duplicate words from a sentence. I will do my best to provide you with a simple method for identifying and removing duplicate words. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. ret = False
  3. def remove_duplicates(sentence):
  4. words = sentence.split(" ")
  5. result = []
  6. for word in words:
  7. if word not in result:
  8. result.append(word)
  9. return " ".join(result)
  14. while True:
  15. print("\n================== Remove all Duplicates Words ==================\n\n")
  19. sentence = "I love coding and coding for sourcecodester"
  21. print("My sentence: ", sentence)
  23. print("After: ", remove_duplicates(sentence))
  25. opt = input("\nDo you want to try again?(yes/no): ")
  27. if opt.lower() == 'yes':
  28. ret=False
  29. elif opt.lower() == 'no':
  30. ret=True
  31. print("Exiting program....")
  33. else:
  34. print("Please enter yes/no:")
  35. break
  37. if ret == False:
  38. continue

This script removes duplicate words from the sentence "I love coding and coding for sourcecodester". It displays the original sentence and then prints the sentence after removing duplicates. The user is prompted to decide if they want to run the process again. If the user chooses to continue, the sentence is processed again; otherwise, the program exits.


There you have it we successfully created How to Remove all Duplicates Words in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment