Python Filtering Fundamentals
Filtering is a fundamental operation in programming, allowing you to select specific elements from a collection based on certain criteria. This challenge will test your ability to implement filtering logic in Python, a skill crucial for data processing, list manipulation, and creating more dynamic applications.
Problem Description
Your task is to implement a function called filter_list that takes two arguments:
data: A list of numbers.condition: A function that accepts a single number as input and returnsTrueif the number meets a specific condition, andFalseotherwise.
The filter_list function should return a new list containing only the numbers from the original data list for which the condition function returns True. The order of elements in the output list should be the same as their order in the input data list.
Key Requirements:
- The function must accept a list of numbers and a function as arguments.
- The function must return a new list. It should not modify the original
datalist. - The filtering logic should be applied using the provided
conditionfunction.
Expected Behavior:
The function should iterate through each element in the data list. For each element, it should call the condition function. If the condition function returns True, the element should be added to the new result list.
Edge Cases:
- An empty input
datalist. - A
conditionfunction that always returnsTrueor always returnsFalse. - A
datalist containing various types of numbers (integers, floats).
Examples
Example 1:
Input:
data = [1, 2, 3, 4, 5, 6]
condition = lambda x: x % 2 == 0 # Filter for even numbers
Output:
[2, 4, 6]
Explanation:
The condition checks if a number is even. The numbers 2, 4, and 6 are even, so they are included in the output list.
Example 2:
Input:
data = [10, -5, 0, 25, -15, 30]
condition = lambda x: x > 0 # Filter for positive numbers
Output:
[10, 25, 30]
Explanation:
The condition checks if a number is greater than 0. The numbers 10, 25, and 30 satisfy this condition.
Example 3:
Input:
data = [1.5, 2.7, 0.9, 3.14, 4.0]
condition = lambda x: x < 3.0 # Filter for numbers less than 3.0
Output:
[1.5, 2.7, 0.9]
Explanation:
The condition checks for numbers less than 3.0. The numbers 1.5, 2.7, and 0.9 meet this criterion.
Example 4: (Edge Case: Empty List)
Input:
data = []
condition = lambda x: x > 10
Output:
[]
Explanation:
When the input list is empty, the function should return an empty list, regardless of the condition.
Constraints
- The
datalist will contain only numeric types (integers and/or floats). - The
conditionfunction will always be a valid Python function that accepts one numeric argument and returns a boolean. - The length of the
datalist can range from 0 to 1000. - The values within the
datalist will be within the range of -1000 to 1000. - Your solution should aim for reasonable efficiency, ideally performing the filtering in a single pass through the data.
Notes
- Consider using a loop to iterate through the
datalist. - Remember to create and return a new list.
- Python's built-in
filter()function is an alternative way to achieve this, but for this challenge, you should implement the logic manually to deepen your understanding.