python 3: generator for map

Question:

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.

Asked By: stepuncius

||

Answers:

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

Answered By: Alex Hall
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.