+ 3

python map function

why output is not coming in map function without using list function

28th Jan 2020, 12:38 PM
Hari Kanna
Hari Kanna - avatar
9 Answers
+ 11
HonFu Lothar Change your print(...) on print(map_) print(*map_) Now you can see clearly what's going on⁉️☺️ 'milk' - I love it‼️☺️😃😁
29th Jan 2020, 10:24 AM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 7
Because the map function returns a map, you need to unpack it into list, tuples e.t.c to see an output
28th Jan 2020, 12:41 PM
Mirielle
Mirielle - avatar
+ 6
By using list, you directly 'milk' the map you created, and create a new list from it. After that, what you do with the old list, won't have an effect.
28th Jan 2020, 6:40 PM
HonFu
HonFu - avatar
+ 5
I have modified the code a bit, and it shows an unexpected behavior: list_ = [1, 2, 3] map_ = list(map(lambda x: x*2, list_)) # added list(....) list_.append(4) print(map_) # removed unpack operator. Any idea HonFu?
28th Jan 2020, 6:37 PM
Lothar
Lothar - avatar
+ 3
Please look at this code: list_ = [1, 2, 3] map_ = map(lambda x: x*2, list_) list_.append(4) print(*map_) One could assume that the output will be [2, 4, 6], because the map was created before I appended 4 to the list. However, the output is 2 4 6 8. So why's that? Map does not create this *now*, it is only a preliminary construction plan for how each value is to be created as soon as the construction is started. When you unpack the map (*), loop over it or make a list with it, only then the values will be created.
28th Jan 2020, 1:47 PM
HonFu
HonFu - avatar
+ 2
John, you're right, it's unpacked of course. 😅 Let me quickly change it.
29th Jan 2020, 9:05 AM
HonFu
HonFu - avatar
+ 1
I agree with @HonFu although the output is 2 4 6 8 on the same line but your explanation was right.
29th Jan 2020, 8:40 AM
John
John - avatar
+ 1
Umar Bello you can use the search at the top so you can get the relevant thread please.
29th Jan 2020, 11:31 AM
John
John - avatar
- 2
How can i creat app in html.
29th Jan 2020, 11:01 AM
Umar Bello
Umar Bello - avatar