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 is a lambda function in Python?
A lambda function is a small anonymous function defined with the keyword lambda. It can take any number of arguments but has only one expression, which is returned.
Click to reveal answer
beginner
What does the filter() function do in Python?
filter() takes a function and a list (or any iterable) and returns a new iterator with only the items for which the function returns True.
Click to reveal answer
beginner
How do you combine lambda and filter() to get even numbers from a list?
Use filter() with a lambda that checks if a number is even: filter(lambda x: x % 2 == 0, numbers)
Click to reveal answer
intermediate
What type of object does filter() return in Python 3?
filter() returns an iterator, which can be converted to a list using list().
Click to reveal answer
intermediate
Why use lambda with filter() instead of a named function?
Using lambda keeps the code short and readable when the filtering logic is simple and used only once.
Click to reveal answer
What does this code return? list(filter(lambda x: x > 3, [1, 4, 2, 5]))
A[1, 2]
B[4, 5]
C[1, 4, 2, 5]
D[3, 4, 5]
✗ Incorrect
The lambda keeps only numbers greater than 3, which are 4 and 5.
Which of these is a correct use of filter() with a lambda to get odd numbers?
Afilter(lambda x: x % 2 == 1, numbers)
Bfilter(lambda x: x % 2 == 0, numbers)
Cfilter(lambda x: x > 2, numbers)
Dfilter(lambda x: x < 0, numbers)
✗ Incorrect
The lambda checks if a number is odd by using x % 2 == 1.
What type of argument does filter() expect as its first parameter?
AA number
BA list
CA string
DA function that returns True or False
✗ Incorrect
filter() needs a function that decides which items to keep.
What will list(filter(lambda x: x, [0, 1, '', 'hello'])) return?
A[1, 'hello']
B[0, 1]
C[0, '', 'hello']
D[]
✗ Incorrect
The lambda returns the item itself, so only truthy values (1 and 'hello') are kept.
Why might you convert the result of filter() to a list?
ABecause <code>filter()</code> returns a dictionary
BBecause <code>filter()</code> returns a string
CBecause <code>filter()</code> returns an iterator, and lists are easier to use and print
DBecause <code>filter()</code> returns a number
✗ Incorrect
filter() returns an iterator, which you often convert to a list to see all items at once.
Explain how to use a lambda function with filter() to select items from a list.
Think about how lambda defines a small function and filter uses it to keep certain items.
You got /3 concepts.
Describe the difference between the output of filter() in Python 2 and Python 3.
Consider what you get directly from filter() and how you can use it.
You got /3 concepts.
Practice
(1/5)
1. What does the following code do? filter(lambda x: x > 5, [2, 7, 4, 10])
easy
A. Selects numbers greater than 5 from the list
B. Selects numbers less than 5 from the list
C. Returns the sum of numbers greater than 5
D. Sorts the list in ascending order
Solution
Step 1: Understand the lambda condition
The lambda function checks if each number is greater than 5.
Step 2: Apply filter with the lambda
filter() keeps only numbers where the lambda returns True, so numbers > 5.
Final Answer:
Selects numbers greater than 5 from the list -> Option A
Quick Check:
filter with lambda x > 5 = select numbers > 5 [OK]
Hint: filter + lambda picks items where condition is True [OK]
Common Mistakes:
Thinking filter sums or sorts the list
Confusing greater than with less than
Assuming filter changes the original list
2. Which of these is the correct syntax to filter even numbers from a list nums using lambda and filter?
easy
A. filter(lambda x: x % 2 == 0, nums)
B. filter(lambda x: x // 2 == 0, nums)
C. filter(lambda x: x % 2, nums)
D. filter(lambda x: x == 2, nums)
Solution
Step 1: Identify condition for even numbers
Even numbers have zero remainder when divided by 2, so x % 2 == 0.
Step 2: Check filter syntax
filter(lambda x: x % 2 == 0, nums) correctly filters even numbers.
Final Answer:
filter(lambda x: x % 2 == 0, nums) -> Option A
Quick Check:
Even check = x % 2 == 0 [OK]
Hint: Use x % 2 == 0 to check even numbers [OK]
Common Mistakes:
Using floor division (//) instead of modulo (%)
Using x % 2 without comparison (filters odd numbers)
Checking equality to 2 instead of remainder zero
3. What is the output of this code?
nums = [1, 3, 6, 8, 11]
result = list(filter(lambda x: x < 7, nums))
print(result)
The condition uses '=' which is assignment, not comparison.
Step 2: Correct operator usage
It should be '==' to compare if x % 2 equals 0.
Final Answer:
Using '=' instead of '==' in lambda condition -> Option C
Quick Check:
Comparison needs '==' not '=' [OK]
Hint: Use '==' for comparison inside lambda [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Forgetting to convert filter result to list
Missing lambda argument
5. You have a list of words: words = ['apple', '', 'banana', ' ', 'cherry', None]. Which code correctly filters out empty strings, strings with only spaces, and None values using lambda and filter?
hard
A. list(filter(lambda x: x == '', words))
B. list(filter(lambda x: x and x.strip(), words))
C. list(filter(lambda x: x is None, words))
D. list(filter(lambda x: x.strip() == '', words))
Solution
Step 1: Understand filtering criteria
We want to remove empty strings (''), strings with only spaces (' '), and None values.
Step 2: Analyze each option
list(filter(lambda x: x and x.strip(), words)) checks x is truthy (not None or empty) and x.strip() removes spaces; if non-empty after strip, it keeps the word.
Final Answer:
list(filter(lambda x: x and x.strip(), words)) -> Option B
Quick Check:
Filter removes empty, spaces, None with x and x.strip() [OK]
Hint: Use x and x.strip() to remove empty/space/None [OK]
Common Mistakes:
Filtering only empty strings but not spaces or None