Build a new Python list when filtering items

← Back to Blog & Articles

Filtering creates a collection containing the values that satisfy a condition. Building a new list gives you a straightforward way to preserve the original input during that process.

Worked example

Suppose a score list contains three, eight, five, and ten. To keep scores of at least six, begin with an empty output list. Visit each score and append it only when it meets the condition. The output contains eight and ten in the same relative order as the input.

What to check

Check that every kept value meets the rule and every discarded value fails it. Test a value exactly on the threshold to distinguish at least six from greater than six. Also test inputs where everything is kept or nothing is kept. Once the loop is clear, a list comprehension can express the same filtering operation more compactly. Understanding the explicit steps first helps you read and check the shorter form.

Continue with the book

Explore the full treatment, worked examples, and exercises in Code Quest with Python.


Companion article for Code Quest with Python, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top