I have a problem as I am a total beginner to Python, now learning text-manipulation. I am trying to extract the five longest words in a text, create a list with those items and order them alphabetically. Then I need to print the results.
How do I proceed?

- 1
- 1
-
1Please provide enough code so others can better understand or reproduce the problem. – Community Sep 30 '21 at 15:39
2 Answers
strings = input("Enter a Text: ").split() # split the text into a list of words
sorted_list = list(sorted(strings, key = len,reverse=True))[:5] # sort the list by length of the words
ans = sorted(sorted_list) # sort the list alphabetically
print(ans) # print the list
Thank you for the input. I tried to do so but still I am doing something wrong. Now I started to create a list instead to convert it to a set, as set does not allow duplicates. Then I convert it back to a list to sort it based on how many characters the text have. The last thing I need to do is to pick the 5 first item of the list. Right now it looks like this, and it does not work. What am I missing?
#creating a list
myTopFiveList = []
#converting the list to a set, as it does not allow duplicates
my_set = set(myTopFiveList)
#converting it back to a list
myTopFiveList = list(my_set)
my_set.add(text)
print(list(my_set))
#sort the new list based on the number of characters
myTopFiveList.sort(key=lambda x: len(x.split(" ")), reverse=True)
#sort the new list based on the number of characters
total = 0 for text in text: total += len(text)
print (total)
#pick the first 5 items of the list

- 1
- 1
-
1Please add this to the original question and format the code to make it easier to read. – eglease Sep 24 '21 at 14:43