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 lessonRefactor 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
- Square Every NumberBeginner
- Filter Out Short WordsBeginner
- Predict the Pipeline OutputBeginner
- Fix the Broken PipelineIntermediate
- Find the Product with reduce()Intermediate
- Build a Data PipelineAdvanced
All Python exercises · 50 practice problems · Functions quiz