• We just launched and are currently in beta. Join us as we build and grow the community.

How to Remove all Duplicates Words in Python

BrownGrilledBoy

B2B Revenue Strategist
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
115
Likes
182
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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 https://www.python.org/downloads/.

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

  2. def

    remove_duplicates(

    sentence)

    :
  3. words =

    sentence.split

    (

    " "

    )
  4. result =

    [

    ]
  5. for

    word in

    words:
  6. if

    word not

    in

    result:
  7. result.append

    (

    word)
  8. return

    " "

    .join

    (

    result)




  9. while

    True

    :
  10. print

    (

    "\n

    ================== Remove all Duplicates Words ==================\n

    \n

    "

    )



  11. sentence =

    "I love coding and coding for sourcecodester"

  12. print

    (

    "My sentence: "

    ,

    sentence)

  13. print

    (

    "After: "

    ,

    remove_duplicates(

    sentence)

    )

  14. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  15. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  16. ret=

    False
  17. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  18. ret=

    True
  19. print

    (

    "Exiting program...."

    )

  20. else

    :
  21. print

    (

    "Please enter yes/no:"

    )
  22. break

  23. if

    ret ==

    False

    :
  24. 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.

Output:

how-to-remove-all-duplicates-words-in-python-1.jpg

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top