You have a list of product dicts, each with name, price, and an in_stock boolean. You want only the in-stock ones. You know list comprehensions handle this — but Python also has a built-in called filter(). When would you reach for one versus the other?
Isn't a comprehension strictly better? I've never needed filter() before.
For simple cases, a comprehension is clearer. But filter() shines when the predicate is already a named function you want to pass by reference — no duplication, no lambda wrapper. Here's the syntax:
filter(lambda p: p["in_stock"], products)The first argument is the predicate (returns True to keep), the second is the iterable. filter() returns a lazy iterator — wrap it in list() to materialize.
So the lambda lambda p: p["in_stock"] takes each product and returns the boolean directly? Python treats it as a predicate?
Exactly. p["in_stock"] is already a boolean, so no == True needed. filter() keeps every item where the lambda returns a truthy value. Here's the full function:
def filter_in_stock(products: list) -> list:
return list(filter(lambda p: p["in_stock"], products))list() turns the iterator into a concrete list so the caller can index, print, or iterate over it multiple times.
What happens if in_stock is missing from a dict? Does the filter crash?
p["in_stock"] raises KeyError when the key is absent. For defensive code, use .get("in_stock", False) — missing key treated as out-of-stock. The test cases here assume the key is always present, but in production that .get() is free insurance.
And for an empty input list, filter() returns an empty iterator, which list() wraps as []?
Empty in, empty out. Same graceful behavior as comprehensions. The comprehension form [p for p in products if p["in_stock"]] does the same thing — pick the one that reads better in context.
TL;DR: list(filter(lambda x: cond, xs)) and [x for x in xs if cond] produce identical results.
filter() — functional style, returns a lazy iterator; wrap with list()filter() wins — predicate is already a named function (list(filter(is_valid, xs)))| Comprehension | filter() |
|---|---|
[x for x in xs if c(x)] | list(filter(c, xs)) |
Both are Pythonic. For inline conditions, most style guides prefer the comprehension.
You have a list of product dicts, each with name, price, and an in_stock boolean. You want only the in-stock ones. You know list comprehensions handle this — but Python also has a built-in called filter(). When would you reach for one versus the other?
Isn't a comprehension strictly better? I've never needed filter() before.
For simple cases, a comprehension is clearer. But filter() shines when the predicate is already a named function you want to pass by reference — no duplication, no lambda wrapper. Here's the syntax:
filter(lambda p: p["in_stock"], products)The first argument is the predicate (returns True to keep), the second is the iterable. filter() returns a lazy iterator — wrap it in list() to materialize.
So the lambda lambda p: p["in_stock"] takes each product and returns the boolean directly? Python treats it as a predicate?
Exactly. p["in_stock"] is already a boolean, so no == True needed. filter() keeps every item where the lambda returns a truthy value. Here's the full function:
def filter_in_stock(products: list) -> list:
return list(filter(lambda p: p["in_stock"], products))list() turns the iterator into a concrete list so the caller can index, print, or iterate over it multiple times.
What happens if in_stock is missing from a dict? Does the filter crash?
p["in_stock"] raises KeyError when the key is absent. For defensive code, use .get("in_stock", False) — missing key treated as out-of-stock. The test cases here assume the key is always present, but in production that .get() is free insurance.
And for an empty input list, filter() returns an empty iterator, which list() wraps as []?
Empty in, empty out. Same graceful behavior as comprehensions. The comprehension form [p for p in products if p["in_stock"]] does the same thing — pick the one that reads better in context.
TL;DR: list(filter(lambda x: cond, xs)) and [x for x in xs if cond] produce identical results.
filter() — functional style, returns a lazy iterator; wrap with list()filter() wins — predicate is already a named function (list(filter(is_valid, xs)))| Comprehension | filter() |
|---|---|
[x for x in xs if c(x)] | list(filter(c, xs)) |
Both are Pythonic. For inline conditions, most style guides prefer the comprehension.
Write `filter_in_stock(products)` that returns a list of products where `in_stock` is True. Use `filter()` with a lambda predicate, then wrap the result in `list()`.
Tap each step for scaffolded hints.
No blank-editor panic.