-2

I am creating a list in Python using .split. My code splits the user's input into a list. I am wondering on how I can stop the program printing duplicates of the word in the sentence. My code is currently this:

sentence = input("Enter a sentence").lower()
sentencesplit = sentence.split()
print(sentencesplit)

When I enter "Run run Forrest run" it will return:

['run', 'run', 'Forrest', 'run']

But I want it to return only run and Forrest, I want it to only list the same word once. Does anyone know a way of doing this? I need to keep the order, so the question's similar to this do not help me, so don't mark this a duplicate, just leave and move on with your life.

Thanks, Izaak

I.Hull
  • 11
  • 5

2 Answers2

2

You can use a set:

list(set(sentencesplit))
Martinbaste
  • 416
  • 2
  • 8
-1
mylist = [word for word in mylist if mylist.count(word) == 1]
blue_note
  • 27,712
  • 9
  • 72
  • 90