Reading Time: 1 minutes
Sorted Sequence With No Duplicates in Python: A Python script which prompts the user for a sequence of space-separated words, and outputs the sorted sequence with no duplicates. For example, if the user inputs 'apple aardvark apple ball cat', then the output should be 'aardvard apple ball cat'. You may use builtin sort() to sort the words.
Sorted Sequence With No Duplicates in Python
sequence = input("Please enter a sequence of space-separated words: ") # apple aardvark apple ball cat words = sequence.split(" ") # ['apple', 'aardvark', 'apple', 'ball', 'cat'] uniqueWords = set(words) # {'aardvark', 'ball', 'apple', 'cat'} sortedWords = sorted(uniqueWords) # ['aardvark', 'apple', 'ball', 'cat'] print(" ".join(sortedWords)) # aardvark apple ball cat
Try it here.