It's the first time I am working with Python for a Uni assignment and I am facing a difficulty regarding a task that says:
Extract five longest words in the entire text, create a List with those items and order them alphabetically. Print out these results on the screen (e.g., ‘Five longest words in this text ordered alphabetically are: “word1”, “word2”, “word3”, “word4”, “word5”’)
So, the code I created so far is:
longest_string = sorted(word_list, key=len)
print(longest_string)
second_longest_string = sorted(word_list, key=len)[-2]
print(second_longest_string)
third_longest_string = sorted(word_list, key=len)[-3]
print(third_longest_string)
fourth_longest_string = sorted(word_list, key=len)[-4]
print(fourth_longest_string)
fifth_longest_string = sorted(list_1, key=len)[-5]
print(fifth_longest_string) ```
I thought I could start by this and then proceed to the alphabetical order, but it looks like this code generates a different output every time, because inside the list there are many strings that have the same number of words.
Any idea how I can proceed?
This will sort the word list based on length and then get 5 most lengthy words and then sort them based on alphabatical order
sorted_words = sorted(sorted(word_list, key=len)[-5:])