map with list
Just as with the range
object, here with the map
object too you can use the list
function to convert all the values at once.
However, there are two advantages of keeping it as a map object and not using list
to flatten it into a list
.
- Memory usage.
- Delayed (lazy) execution of code.
def double(num):
return 2 * num
numbers = [7, 2, 4, 1]
double_numbers = list(map(double, numbers))
print(double_numbers)
Output:
[14, 4, 8, 2]