Challenge - 5 Problems
Lambda Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this code using lambda and filter?
Consider the following Python code that uses
lambda with filter(). What will be printed?Python
numbers = [1, 2, 3, 4, 5, 6] result = list(filter(lambda x: x % 2 == 0, numbers)) print(result)
Attempts:
2 left
๐ก Hint
Remember, filter keeps items where the lambda returns True.
โ Incorrect
The lambda function checks if a number is even (x % 2 == 0). filter() keeps only those numbers. So the output is the list of even numbers: [2, 4, 6].
โ Predict Output
intermediate2:00remaining
What does this filter with lambda return?
What will be the output of this code snippet?
Python
words = ['apple', 'banana', 'cherry', 'date'] result = list(filter(lambda w: len(w) > 5, words)) print(result)
Attempts:
2 left
๐ก Hint
Check the length of each word carefully.
โ Incorrect
The lambda keeps words with length greater than 5. 'banana' and 'cherry' have length 6, so they are kept.
โ Predict Output
advanced2:00remaining
Output of filter with lambda checking nested list elements
What is the output of this code?
Python
data = [[1, 2], [3, 4], [5], []] result = list(filter(lambda x: len(x) == 2, data)) print(result)
Attempts:
2 left
๐ก Hint
Filter keeps lists with exactly two elements.
โ Incorrect
Only the first two lists have length 2, so they are kept.
โ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code produce when run?
Python
numbers = [1, 2, 3] result = list(filter(lambda x: x > 1, numbers)) print(result[3])
Attempts:
2 left
๐ก Hint
Check the length of the filtered list and the index accessed.
โ Incorrect
The filtered list is [2, 3], which has indices 0 and 1. Accessing index 3 causes IndexError.
๐ง Conceptual
expert3:00remaining
Which option produces the filtered list of odd squares?
Given
nums = range(1, 6), which option produces a list of squares of odd numbers only using filter() and lambda?Attempts:
2 left
๐ก Hint
Filter after squaring, check odd squares.
โ Incorrect
Option B filters the squared numbers to keep only odd squares. Option B filters by sqrt parity using floating-point modulo, which is unreliable due to precision issues. Option B filters nums, not squares. Option B filters nums, not squares.