Editor

Refactor to a List Comprehension

Advanced · Refactor · 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

Refactor this map/filter pipeline into a single list comprehension that produces the same output.

Original code:

words = ["Hello", "WORLD", "Python", "CODE", "test"]
result = list(map(str.lower, filter(lambda w: len(w) > 4, words)))

Write a list comprehension that produces the same result and print it.

Expected output:

['hello', 'world', 'python']
words = ["Hello", "WORLD", "Python", "CODE", "test"]
# Refactor to a list comprehension
result = [] # Replace with your comprehension
print(result)

More Map, Filter and Reduce exercises

All Python exercises · 50 practice problems · Functions quiz