Python: How to remove duplicates from a list and print out the new list in order -


this question has answer here:

basically, have list following words

['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you', 'ask', 'what', 'you', 'can', 'do', 'for', 'your', 'country']  

(this example given me teacher , pretty testing).

i want figure out how remove duplicates , print out new list without said duplicates, output should

['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you']  

after trying use set(), managed remove duplicates, prints out sentence in random order, quite bothersome.

here code:

sentence = input('please enter sentence. no punctuation may included: enter code here').lower().split() print('this sentence:', sentence) no_duplicates = list(set(sentence)) print('this sentence without duplicates', no_duplicates) 

by way, incase didn't notice, i'm still quite newb coding try , keep answers simple may understand you're saying.

sets not random, unordered. means can't use them in use case, because order important in task.

however, there simple solution using sets. since homework, won't give code, algorithm start empty set, iterate through each word, checking if in set: if isn't, add set , output list, otherwise skip it.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -