filter() function in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run the filter() function changes as the input list gets bigger.
Specifically, how does the number of items affect the work done inside filter()?
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered = list(filter(lambda x: x % 2 == 0, numbers))
print(filtered)
This code filters out even numbers from a list of numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
filter()function checks each item in the list one by one. - How many times: It runs the check exactly once for every item in the list.
As the list gets bigger, the number of checks grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of items. Double the items, double the checks.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the size of the list.
[X] Wrong: "The filter function only checks some items, so it runs faster than looking at every item."
[OK] Correct: The filter function must look at every item to decide if it passes the test or not, so it always checks all items.
Understanding how filter() works helps you explain how your code handles data efficiently, a useful skill in many coding discussions.
"What if the filtering function itself took longer to run for each item? How would that affect the overall time complexity?"
Practice
filter() function do in Python?Solution
Step 1: Understand the purpose of
Thefilter()filter()function takes a function and a list, then keeps only items where the function returns True.Step 2: Compare options with the purpose
Only Selects items from a list that meet a condition describes selecting items based on a condition, which matchesfilter()'s job.Final Answer:
Selects items from a list that meet a condition -> Option CQuick Check:
filter()selects items [OK]
- Thinking filter changes items instead of selecting
- Confusing filter with map or sort
- Assuming filter adds or combines items
filter() to keep even numbers from a list nums?Solution
Step 1: Recall
The correct syntax isfilter()syntaxfilter(function, iterable), where function tests each item.Step 2: Check each option
filter(lambda x: x % 2 == 0, nums) useslambda x: x % 2 == 0as function andnumsas iterable, which is correct.Final Answer:
filter(lambda x: x % 2 == 0, nums) -> Option AQuick Check:
filter(function, iterable) correct order [OK]
- Swapping function and iterable arguments
- Using expression instead of function
- Missing lambda or function for filtering
nums = [1, 2, 3, 4, 5] even_nums = list(filter(lambda x: x % 2 == 0, nums)) print(even_nums)
Solution
Step 1: Understand the filter condition
The lambda function keeps numbers wherex % 2 == 0, meaning even numbers.Step 2: Apply filter to the list
From[1, 2, 3, 4, 5], only 2 and 4 are even, so the filtered list is[2, 4].Final Answer:
[2, 4] -> Option BQuick Check:
Filter keeps even numbers [OK]
- Confusing even and odd numbers
- Forgetting to convert filter to list
- Expecting original list unchanged
nums = [10, 15, 20] result = filter(x % 10 == 0, nums) print(list(result))
Solution
Step 1: Check filter function argument
The first argument tofilter()must be a function, butx % 10 == 0is an expression, not a function.Step 2: Identify fix
We need to wrap the expression in a lambda:lambda x: x % 10 == 0to make it a function.Final Answer:
Missing lambda function for filter -> Option DQuick Check:
filter needs a function as first argument [OK]
- Passing expression instead of function
- Assuming filter returns list directly
- Ignoring syntax errors in lambda usage
words = ['apple', '', 'banana', None, 'cherry', '']. Which code correctly filters out empty strings and None values using filter()?Solution
Step 1: Understand filtering out empty and None
Empty strings and None are 'falsy' in Python, solambda w: wkeeps only truthy values.Step 2: Check each option
list(filter(lambda w: w, words)) keeps only truthy values, removing empty strings and None. Options B and C keep only empty or None, which is opposite. list(filter(lambda w: w != None or w != '', words)) uses wrong logic and keeps all.Final Answer:
list(filter(lambda w: w, words)) -> Option AQuick Check:
filter withlambda w: wremoves falsy values [OK]
lambda x: x to remove falsy values [OK]- Using wrong condition to keep empty or None
- Using 'or' instead of 'and' in condition
- Expecting filter to remove without function
