Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Partition a list by a condition

Split a list into two lists based on a predicate function.

Python practice18 minLists & TuplesIntermediateLast updated March 17, 2026

Problem statement

Implement partition(lst, predicate) which takes a list and a predicate (a function that returns True or False for an element). Return a tuple of two lists: (true_list, false_list). true_list contains elements for which predicate(element) is True, in their original order; false_list contains the rest, also preserving order. The function should work for any callable predicate.

Task

Practice using higher-order functions and list construction to separate elements that match a condition from those that don't.

Examples

Partition evens and odds

Input

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

Output

([2, 4], [1, 3, 5])

Even numbers go to the first list, odd numbers to the second, preserving order.

Input format

Two arguments: a list and a predicate function (e.g., lambda x: x>0).

Output format

A tuple of two lists: (elements_where_predicate_true, elements_where_predicate_false).

Constraints

Do not modify the input list in-place. Preserve the relative order of elements in both output lists.

Samples

Sample 1

Input

partition([0, -1, 2], lambda x: x >= 0)

Output

([0, 2], [-1])

0 and 2 satisfy the predicate, -1 does not.