1

I've got trouble with using generator on map object. This is the simpliest example:

a = ['1','2','3']
a = map(int, a)
for x in a:
    print(x, end = ' ') #output 1 2 3
b = [x for x in a]
print(b) #output []

Python 3.5
P.S. Of course I know about list(map) but I want to know why this doesn't work.

stepuncius
  • 266
  • 1
  • 4
  • 17

1 Answers1

3

Because the map object got exhausted after your printed everything in it. You can only iterate through it once.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89