0
what does map function do in this code?
def add_five(x): return x +5 nums = [11, 22, 33, 44, 55] result = list(map(add_five, nums)) print(result)
3 Respostas
+ 3
map() => Executes a function for each item of an iterable and returns a map object.
In your code for each value of nums add_five() function is being executed.
+ 2
The map function takes a function as its first argument and an iterable as its second argument. Each element of the iterable is passed to the function and an iterable map object is then returned containing the elements returned from the function.
In this case each element is passed to the add_five function where 5 is added to it and then returned into a new map object. That object is then converted back into a list so that the result list is;
[16, 27, 38, 49, 60]
0
According to add_five function defination, the list nums are mapped each of its value.
So adds 5 to each value and returned values are defined as a list..