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]))✗ 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?✗ 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?✗ Incorrect
filter() needs a function that decides which items to keep.What will
list(filter(lambda x: x, [0, 1, '', 'hello'])) return?✗ 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?✗ 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.