Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Sort words by length using a lambda key

Sort a list of words by their length using a lambda as the key.

Python practice8 minFunctions as Objects (Lambda, Map/Filter)BeginnerLast updated March 20, 2026

Problem statement

Given a list of strings, return a new list with the same strings sorted by their length in ascending order. Use a lambda expression as the key to sorted(). The sort should be stable, so words with the same length keep their original relative order.

Task

Use sorted() with a lambda key to order words by length while preserving stability for ties.

Examples

Basic example

Input

['apple', 'fig', 'banana']

Output

['fig', 'apple', 'banana']

Lengths are 5, 3, 6. Sorted ascending by length yields ['fig', 'apple', 'banana'].

Input format

A single argument: a list of strings, e.g. ['a', 'bb', 'ccc'].

Output format

A list of strings sorted by length, e.g. ['a', 'bb', 'ccc'].

Constraints

Do not modify the input list in-place; return a new list. Use a lambda with the key parameter of sorted().

Samples

Sample 1

Input

['cat', 'elephant', 'dog']

Output

['cat', 'dog', 'elephant']

Lengths are 3, 8, 3. Stable sort preserves original relative order for 'cat' and 'dog'.