Recall & Review
beginner
What does the
map() function do in Python when used with lists?The
map() function applies a given function to each item of a list (or other iterable) and returns a new iterable with the results.Click to reveal answer
beginner
How do you convert the result of
map() into a list?You wrap the
map() call with list(), like list(map(function, iterable)), to get a list of the mapped results.Click to reveal answer
beginner
Example: What is the output of
list(map(lambda x: x * 2, [1, 2, 3]))?The output is
[2, 4, 6] because each element is multiplied by 2.Click to reveal answer
intermediate
Can
map() be used with multiple iterables? How?Yes. You can pass multiple iterables to
map() and the function must accept that many arguments. It applies the function element-wise across the iterables.Click to reveal answer
intermediate
Why might
map() be preferred over a for-loop for element-wise operations?map() is often more concise and can be faster because it applies the function in a clean, functional style without explicit loops.Click to reveal answer
What does
map() return in Python 3?✗ Incorrect
map() returns a map object, which is an iterator. You can convert it to a list if needed.How do you apply a function to each element of a list using
map()?✗ Incorrect
The correct syntax is
map(function, iterable).Which of these is a valid use of
map() with multiple iterables?✗ Incorrect
When using multiple iterables, the function must accept the same number of arguments.
What will
list(map(str, [1, 2, 3])) produce?✗ Incorrect
It converts each number to a string, resulting in a list of strings.
Why might you choose
map() over a list comprehension?✗ Incorrect
map() can be clearer when applying a named function to each element.
Explain how the
map() function works for element-wise mapping in Python.Think about how you apply a function to each item in a list.
You got /4 concepts.
Describe a situation where using
map() with multiple iterables is useful.Imagine adding two lists element by element.
You got /4 concepts.