Recall & Review
beginner
What is a
lambda function in Python?A
lambda function is a small anonymous function defined with the lambda keyword. It can take any number of arguments but has only one expression, which is returned.Click to reveal answer
beginner
What does the
map() function do in Python?map() applies a given function to each item of an iterable (like a list) and returns a map object with the results.Click to reveal answer
beginner
How do you combine
lambda and map() to square each number in a list?You can write:
list(map(lambda x: x**2, [1, 2, 3])) which returns [1, 4, 9].Click to reveal answer
intermediate
Why use
lambda with map() instead of a regular function?Using
lambda with map() lets you write quick, simple functions inline without defining a separate function with def.Click to reveal answer
beginner
What type of object does
map() return and how do you get a list from it?map() returns a map object, which is an iterator. To get a list, wrap it with list(), like list(map(...)).Click to reveal answer
What does this code return?
list(map(lambda x: x + 1, [1, 2, 3]))✗ Incorrect
The lambda adds 1 to each element, so 1+1=2, 2+1=3, 3+1=4.
Which of these is a correct way to use
map() with a lambda to double numbers?✗ Incorrect
Option B uses Python lambda syntax correctly to double each number.
What is the output type of
map() before converting it?✗ Incorrect
map() returns a map object, which is an iterator.Why might you prefer
lambda with map() over a for-loop?✗ Incorrect
Using
lambda with map() makes code shorter and clearer for simple tasks.What will this code output?
list(map(lambda x: x % 2 == 0, [1, 2, 3, 4]))✗ Incorrect
The lambda checks if each number is even, returning True or False accordingly.
Explain how
lambda functions work with map() in Python.Think about how you can quickly change each item in a list without writing a full function.
You got /4 concepts.
Describe a real-life example where using
lambda with map() would be helpful.Imagine you want to quickly add tax to a list of prices.
You got /4 concepts.