Python - same instruction, different result

Can someone help me understand what is going on in the following Python code (python 3.2)? I'm really clueless here.

import sys u = sys.stdin.readline() # try entering the string "1 2 3" r = map(lambda t: int(t.strip()),u.split()) print(sum(r)) # prints 6 print(sum(r)) # prints 0 ? 

Thanks.

+7
source share
1 answer

map() in Python 3.x returns an iterator, not a list. Passing through sum() , it consumes it the first time, leaving nothing for the second time.

+14
source

All Articles