Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Filter values using a lambda condition

Create a reusable filter function that accepts a predicate and returns filtered results.

Python practice19 minFunctions as Objects (Lambda, Map/Filter)IntermediateLast updated March 20, 2026

Problem statement

Implement filter_by_predicate(values, predicate) that returns a list containing only those items from values for which predicate(item) is truthy. Use the built-in filter function and ensure the original order is preserved.

Task

Use filter with a function (often a lambda) passed as an argument to selectively keep items from a list.

Examples

Filter evens

Input

filter_by_predicate([1,2,3,4], lambda x: x%2==0)

Output

[2, 4]

Only even numbers remain.

Input format

values: list of items. predicate: a callable that accepts one argument and returns a truthy/falsy value.

Output format

A list of items from values for which predicate returned truthy, preserving input order.

Constraints

Use filter (and list(...) to realize the result). Do not use explicit loops.

Samples

Sample 1

Input

filter_by_predicate(['apple','pear','apricot'], lambda s: s.startswith('a'))

Output

['apple', 'apricot']

Keeps strings that start with 'a'.