Function for compressing a list based on a condition
Python
Medium
functions
Task «Function for compressing a list based on a condition»
Write a function `compress_list` that takes a list of integers and a condition function (predicate). The function should remove all elements from the list that do not satisfy the condition and return a new list, preserving the order. If the original list is empty, return an empty list. The condition is specified by a function that returns True or False.
Input Format
First line: integer N (0 ≤ N ≤ 1000) — the number of elements in the list. Then N lines with integers — the elements of the list. Last line: integer K (0 or 1), where 0 means the condition "the number is even", 1 means "the number is positive".
Output Format
Output: a string with the elements of the resulting list, separated by a space. If the list is empty, output an empty string.
Examples
Example 1
INPUT
6
1 2 3 2 1 4
OUTPUT
1 2 3 4
main.py