Python @ DjangoSpin

PyPro #26 Sorted Sequence With No Duplicates

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
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.

See also:


Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon