list1 = [1,2,3,4,5,6,7,8,9,10]
squared = map(lambda x:x**2, filter(lambda x:x%2==0, list1))
print(squared)
The output i expect is
Output:
4
However, it comes as
Output:
<map object at 0x1076aa150>
Why does this happen? I went through several sites, I tried running code. According to
this site, the code is
# Python program to demonstrate working
# of map.
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
And the output indeed is as expected,
Output:
[2, 4, 6, 8]
I'm confused, why am I getting this problem, and what is it's cause? Any help would be appreciated
map()
returns
map
object. The other snippet converts the
map
object to list on line 11. You don't do that.
print(squared)
vs
print(list(result))
The idea here is that of a lazy sequence - evaluation of the items doesn't happen until you ask for it. One of the things this allows you to do is have potentially infinite sequences. There's a bit of a comparison between Python and Clojure
here and it includes references to more pages about laziness.
A lot more stuff got lazy

in python 3.
@
ndc85430 explain why is it so.
If i run code in Python 2.7 this happens.
>>> list1 = [1,2,3,4,5,6,7,8,9,10]
>>> squared = map(lambda x:x**2, filter(lambda x:x%2==0, list1))
>>> print(squared)
[4, 16, 36, 64, 100]
Thanks a lot! However, I never knew python 3 was this lazy :D