+ 3
python map function
why output is not coming in map function without using list function
9 ответов
+ 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.
+ 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?
+ 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.
+ 2
John, you're right, it's unpacked of course. 😅 Let me quickly change it.
+ 1
I agree with @HonFu although the output is 2 4 6 8 on the same line but your explanation was right.
+ 1
Umar Bello you can use the search at the top so you can get the relevant thread please.
- 2
How can i creat app in html.