map in Python 3 vs Python 2
map
returns an iterator. As such, its output may only be used once. If you wish to store your results in a list, in the same way as Python 2.x, simply call list
when you use map
:
L = list(map(lambda x:2**x, range(7)))
The list L
will now contain your results however many times you call it.
The problem you are facing is that once map
has iterated once, it will yield nothing for each subsequent call. Hence you see an empty list for the second call.
For a more detailed explanation and advice on workarounds if you cannot exhaust your iterator but wish to use it twice, see Why can't I iterate twice over the same data.