When I do this at command line (same for filter and reduce)
map( lambda x: x+1, [1,2,3,4,5] )
instead of a list/collection as a result... i got
<map object at 0x6ffffe7b630>
to obtain the list I have to apply the list() function same happens if I use a plain old function to replace the lambda...
Why this behavior ?
Because many of the iteration functions use 'lazy' evaluation. Namely, they don't apply the function immediately all elements of the list. Instead they use a coroutine to apply the function one at a time as the "map object" is iterated.
This is a performance feature when some (but not all) of the items will be iterated, or might not need to be determined all at once. The lambda is applied only as needed. Previous versions of python didn't have this optimization and instead just applied the lambda to everything once map is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With