Sort Words by Length in Python

  • Share this:

Code introduction


This function takes a list of words and returns a new list with the words sorted by length.


Technology Stack : re, time, math, random, sys, os

Code Type : Function

Code Difficulty : Intermediate


                
                    
import re
import time
import math
import random
import sys
import os

def sort_words_by_length(words):
    """对单词列表按长度排序

    Args:
        words (list): 单词列表

    Returns:
        list: 排序后的单词列表
    """
    return sorted(words, key=len)