Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Filter a list using a predicate

Return a new list containing only the elements that satisfy a given predicate function.

Python practice13 minLists & TuplesIntermediateLast updated March 17, 2026

Problem statement

Given a list and a predicate (a callable that accepts a single argument and returns a truthy/falsy value), produce a new list containing only the elements for which the predicate returns a truthy value. Do not modify the original list. The predicate will be provided as a function or lambda. Preserve the original order of elements.

Task

Implement filter_list(lst, predicate) that returns a list of elements for which predicate(element) is truthy.

Examples

Filter even numbers

Input

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

Output

[2, 4]

The predicate keeps only numbers divisible by 2.

Input format

A list (lst) and a predicate function taking one argument.

Output format

A list containing elements from lst for which predicate(element) is truthy.

Constraints

Do not use external libraries. The predicate will be a callable. Maintain element order. Time complexity should be O(n).

Samples

Sample 1

Input

filter_list(['apple', 'banana', 'avocado'], lambda s: s.startswith('a'))

Output

['apple', 'avocado']

Keeps only strings that start with 'a'.