what does map function do in this code? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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)

20th Jun 2021, 11:54 AM
Likhon
Likhon - avatar
3 Antworten
+ 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.
20th Jun 2021, 12:02 PM
TOLUENE
TOLUENE - avatar
+ 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]
20th Jun 2021, 12:04 PM
ChaoticDawg
ChaoticDawg - avatar
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..
20th Jun 2021, 12:01 PM
Jayakrishna 🇮🇳