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.
Full lesson preview
Split a list into two lists based on a predicate function.
Problem statement
Task
Examples
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
Output format
Constraints
Samples
Input
partition([0, -1, 2], lambda x: x >= 0)
Output
([0, 2], [-1])
0 and 2 satisfy the predicate, -1 does not.