3

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 ?

ZEE
  • 2,931
  • 5
  • 35
  • 47
  • Strongly disagree with the 'duplicate' mark... my question has a technical aspect to be explained... the question you pointed just says how to use the method... – ZEE Feb 10 '16 at 19:06
  • Not a problem, apologies. – alecxe Feb 10 '16 at 19:08
  • 1
    The accepted answer to that question says _"In Python 3+, many processes that iterate over iterables return iterators themselves"_. That's the technical reason. – tdelaney Feb 10 '16 at 19:12

2 Answers2

2

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.

Chad S.
  • 6,252
  • 15
  • 25
  • Can you provide a proof of the function being lazy? As far as I know, in the case of `map()` the algorithm is substantially different to, say, a `for` loop. The former is some 250 million times faster. A test of `map()` on a list of 50 million rows, takes 450 ns, and the results are immediately available. On the other hand, a `for` loop on the same data takes some 1.1 minute. – Pouria Feb 10 '16 at 19:19
  • I agree the algorithm is different. There may be a bit of a nomenclature misunderstanding about what I mean by "lazy". I mean that it only does work as needed. It doesn't loop over the whole list and apply lambda at each element when `map` is called instead it waits for the map object to be iterated and applies the lambda to each element as the iterator would yield it. – Chad S. Feb 10 '16 at 19:25
  • Also, downvoting by way of retribution is a sure way to ensure that people don't respond to you the next time you ask why you've been downvoted. – Chad S. Feb 10 '16 at 19:26
  • Just disagree with your answer, sorry. I don't see a proof. I respected your opinion, I expect you respect mine. You're welcome not to answer. – Pouria Feb 10 '16 at 19:31
  • Lazy here means "delayed execution until need"... not that the code is bad... LOL ;-) – ZEE Feb 10 '16 at 19:35
  • Thank you for telling me what lazy means. I know that after 15 years of programming. What I'm saying is that this is not the reason. – Pouria Feb 10 '16 at 19:39
  • Your stubborn insistence notwithstanding.. The above is the reason. https://docs.python.org/3/whatsnew/3.0.html#views-and-iterators-instead-of-lists https://www.python.org/dev/peps/pep-0289/ – Chad S. Feb 10 '16 at 19:47
1

The function map doesn't return a list, but an iterator

Return an iterator that applies function to every item of iterable, yielding the results.

The documentation for map has more information.

Michal Frystacky
  • 1,418
  • 21
  • 38