Technical skill practice

Python filter(): Select Readings without Losing Valid Zero

Python filter returns an iterator over items whose predicate is true. It selects original items rather than calculating replacement values. Use an explicit predicate when zero is a valid item, because filter(None, values) uses truthiness and would discard zero along with other false values.

Start with a small, explicit task

Keep nonnegative readings from a fictional sequence containing a negative value and a missing marker. Trace which inputs the predicate has inspected, then compare the explicit selection with truthiness filtering. The missing marker is None by the stated fixture contract.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_filter.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

0 [4, 7]

[-2, 0, 4, None, 7]

[4]

inspected = []
def acceptable(value):
    inspected.append(value)
    return value is not None and value >= 0
readings = [-2, 0, 4, None, 7]
chosen = filter(acceptable, readings)
assert inspected == []
first = next(chosen)
remaining = list(chosen)
assert first == 0 and remaining == [4, 7]
assert inspected == readings
assert list(chosen) == []
assert list(filter(None, [0, 4, None])) == [4]
print(first, remaining)
print(inspected)
print(list(filter(None, [0, 4, None])))

Explain the result

Creating the filter does not inspect the inputs. next skips the rejected -2 and returns the first accepted item, zero. Consuming the remainder retains 4 and 7. The trace eventually contains every input because finding all accepted values requires checking the entire finite source. The exhausted iterator does not restart.

The predicate checks for None before performing the numeric comparison, so the missing marker does not cause a comparison error. The separate truthiness example removes zero, demonstrating why its simpler-looking rule would be wrong for these readings. The retained numbers themselves are unchanged; use a transformation such as map when you need new numeric values.

Avoid these mistakes

Do not assume a lazy iterator is a reusable result collection. Materialize the accepted values when you need repeated access and the size is appropriate. Keep side effects out of production predicates when possible; the inspection trace here exists to make evaluation order observable.

Questions, answered

Will filter transform accepted values?

No. It retains items from the source; this example keeps zero, 4 and 7 unchanged.

Why not use filter(None, readings)?

That rule removes false values, including a legitimate numeric zero, instead of applying the stated nonnegative-reading rule.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.