I wanted to calculate the sum of squares up to n. Say n is 4. Then this code generates a list a map object in the range 0 to 4:
m = map(lambda x: x**2, range(0,4))
Ease enough. Now call list on m, and then sum:
>>> sum(list(m))
14
The unexpected behavior is that if I run the last line again, the sum is 0:
>>> sum(list(m))
0
I suspect that this is because calling list(m)
returns an empty list, but I can't find an explanation for this behavior. Can someone help me out with this?