Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a reusable filter function with lambda

Create a factory that returns reusable filter functions (optionally negated) to apply to any iterable.

Python practice32 minFunctions as Objects (Lambda, Map/Filter)AdvancedLast updated March 20, 2026

Problem statement

Filtering logic is frequently reused across different parts of a program. Rather than repeating predicates and list comprehensions, build a factory that produces reusable filter functions: - make_filter(predicate, negate=False) should return a function that accepts an iterable and returns a list of items where predicate(item) is True (or False if negate is True). - The returned function must accept any iterable (including generators) and always return a list. This pattern makes predicates first-class values that can be stored and reused.

Task

Implement make_filter(predicate, negate=False) that returns a callable expecting an iterable and returning a filtered list.

Examples

Keep even numbers

Input

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

Output

[2, 4]

The produced function filters the iterable to include only even numbers.

Input format

Two parameters to the factory: predicate (callable) and optional negate (bool). The returned function accepts one parameter: an iterable.

Output format

The returned function produces a Python list of filtered items.

Constraints

- The returned function must accept any iterable and must return a list. - Do not use print; just return the filtered list.

Samples

Sample 1

Input

make_filter(lambda s: 'ap' in s.lower())(['Apple','banana','Grape'])

Output

['Apple', 'Grape']

Case-insensitive substring match; 'Apple' and 'Grape' include 'ap'.