Skip to content Skip to sidebar Skip to footer

How To Sort A List Of Words By Length

I want to sort words in a list based on length. The test case is {'cat','jump','blue','balloon'} When run through this method it will print something along the lines of: {'balloon'

Solution 1:

  • print is a function in Python 3.5 and needs to be used as print(words). Read more about it here
  • Given a list of words this can be used to sort them according to length of string using sorted:

    sorted(word_list , key = len)

Solution 2:

Try this. It will sort the list based on length of strings in ascending order

list_name.sort(key=len)

For descending order,

list_name.sort(key=len,reverse=True)

Post a Comment for "How To Sort A List Of Words By Length"