Read a list comprehension
The function below cleans a sequence of text lines. Trace the filter and the transformation separately.
def summarize(lines):
return [line.strip().upper() for line in lines if line.strip()]
The if clause removes blank lines, while the expression before for determines each value in the returned list.
What does summarize return for [" hi ", " ", "Bye"]?
Why it matters: Reading compact transformations accurately helps you predict behavior before changing code.