0
0
Pythonprogramming~5 mins

Lambda with map() in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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]))
A[1, 3, 5]
B[1, 2, 3]
C[0, 1, 2]
D[2, 3, 4]
Which of these is a correct way to use map() with a lambda to double numbers?
Amap(x => x * 2, [1, 2, 3])
Bmap(lambda x: x * 2, [1, 2, 3])
Cmap(lambda x: x + 2, [1, 2, 3])
Dmap(double, [1, 2, 3])
What is the output type of map() before converting it?
Alist
Bdictionary
Cmap object (iterator)
Dstring
Why might you prefer lambda with map() over a for-loop?
AIt allows concise, readable code for simple operations
BIt uses less memory
CIt is always faster
DIt can only be used with lists
What will this code output?
list(map(lambda x: x % 2 == 0, [1, 2, 3, 4]))
A[False, True, False, True]
B[1, 2, 3, 4]
C[True, False, True, False]
D[0, 1, 0, 1]
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.