Python: How to remove duplicates from a list and print out the new list in order -
this question has answer here:
- how remove duplicates list in whilst preserving order? 33 answers
- removing duplicates in lists 30 answers
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
Post a Comment