Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the filter() function do in Python?
The filter() function takes a function and a sequence (like a list) and returns a new sequence with only the items for which the function returns True.
Click to reveal answer
beginner
How do you use filter() with a lambda function to keep only even numbers from a list?
You write: filter(lambda x: x % 2 == 0, my_list) This keeps only numbers divisible by 2 (even numbers).
Click to reveal answer
intermediate
What type of object does filter() return in Python 3?
It returns a filter object, which is an iterator. You can convert it to a list using list() to see the filtered items.
Click to reveal answer
intermediate
What happens if the function passed to filter() is None?
If the function is None, filter() removes all items that are false in a Boolean context (like False, 0, '', None).
Click to reveal answer
beginner
Can filter() be used with any iterable, or only lists?
filter() works with any iterable, such as lists, tuples, sets, or even strings.
Click to reveal answer
What does filter() return in Python 3?
AA dictionary
BA list
CA tuple
DA filter object (an iterator)
✗ Incorrect
filter() returns a filter object, which is an iterator. You can convert it to a list if needed.
What happens if you pass None as the function to filter()?
AIt returns the original iterable unchanged
BIt raises an error
CIt filters out all false values from the iterable
DIt returns an empty list
✗ Incorrect
Passing None makes filter() remove all items that are false in a Boolean context.
Which of these is a correct way to use filter() to keep only positive numbers from a list nums?
Afilter(lambda x: x > 0, nums)
Bfilter(lambda x: x < 0, nums)
Cfilter(lambda x: x == 0, nums)
Dfilter(lambda x: x != 0, nums)
✗ Incorrect
The function lambda x: x > 0 keeps only numbers greater than zero.
How can you see the filtered results from filter()?
APrint the filter object directly
BConvert the filter object to a list using <code>list()</code>
CUse <code>str()</code> on the filter object
DUse <code>dict()</code> on the filter object
✗ Incorrect
The filter object is an iterator, so converting it to a list shows the filtered items.
Can filter() be used on a string?
AYes, because strings are iterable
BNo, strings are not iterable
COnly if converted to a list first
DOnly with a special function
✗ Incorrect
Strings are iterable, so filter() can be used directly on them.
Explain how the filter() function works and give an example using a lambda function to filter even numbers.
Think about how you pick only certain items from a list based on a test.
You got /5 concepts.
What happens when you pass None as the function to filter()? Why might this be useful?
Consider what values are treated as false in Python.
You got /4 concepts.
Practice
(1/5)
1. What does the filter() function do in Python?
easy
A. Sorts the items in a list
B. Changes all items in a list to uppercase
C. Selects items from a list that meet a condition
D. Adds all items in a list together
Solution
Step 1: Understand the purpose of filter()
The 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 matches filter()'s job.
Final Answer:
Selects items from a list that meet a condition -> Option C
Quick Check:
filter() selects items [OK]
Hint: Remember: filter keeps items passing the test [OK]
Common Mistakes:
Thinking filter changes items instead of selecting
Confusing filter with map or sort
Assuming filter adds or combines items
2. Which of these is the correct syntax to use filter() to keep even numbers from a list nums?
easy
A. filter(lambda x: x % 2 == 0, nums)
B. filter(nums, lambda x: x % 2 == 0)
C. filter(x % 2 == 0, nums)
D. filter(lambda x: nums % 2 == 0)
Solution
Step 1: Recall filter() syntax
The correct syntax is filter(function, iterable), where function tests each item.
Step 2: Check each option
filter(lambda x: x % 2 == 0, nums) uses lambda x: x % 2 == 0 as function and nums as iterable, which is correct.
Final Answer:
filter(lambda x: x % 2 == 0, nums) -> Option A
Quick Check:
filter(function, iterable) correct order [OK]
Hint: filter(function, iterable) order matters [OK]
The first argument to filter() must be a function, but x % 10 == 0 is an expression, not a function.
Step 2: Identify fix
We need to wrap the expression in a lambda: lambda x: x % 10 == 0 to make it a function.
Final Answer:
Missing lambda function for filter -> Option D
Quick Check:
filter needs a function as first argument [OK]
Hint: filter needs a function, use lambda for expressions [OK]
Common Mistakes:
Passing expression instead of function
Assuming filter returns list directly
Ignoring syntax errors in lambda usage
5. You have a list of words: words = ['apple', '', 'banana', None, 'cherry', '']. Which code correctly filters out empty strings and None values using filter()?
hard
A. list(filter(lambda w: w, words))
B. list(filter(lambda w: w == '', words))
C. list(filter(lambda w: w is None, words))
D. list(filter(lambda w: w != None or w != '', words))
Solution
Step 1: Understand filtering out empty and None
Empty strings and None are 'falsy' in Python, so lambda w: w keeps 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.