Editor

Filter Out Short Words

Beginner · Write the code · from Python map(), filter(), reduce(), and Higher-Order Functions

From the lesson: Python map(), filter(), reduce(), and Higher-Order Functions

Picture an assembly line in a factory. Raw materials enter at one end. At the first station, each piece is shaped. At the next station, defective pieces are removed. At the final station, all the good pieces are combined into one finished product.

Read the lesson

Use filter() with a lambda to keep only words that have more than 3 characters.

Given: ["hi", "hello", "hey", "world", "ok", "python"]

Print the result as a list.

Expected output:

['hello', 'world', 'python']
words = ["hi", "hello", "hey", "world", "ok", "python"]
# Use filter() to keep words longer than 3 characters

More Map, Filter and Reduce exercises

All Python exercises · 50 practice problems · Functions quiz