1

Does anyone one know why:

x2 = map(lambda x: x, [2, 4, 6])
print(list(x2)) 
print(list(x2))

gives:

[2, 4, 6]
[]

It is as if I can only use list once on the map object. I do not understand what is happening.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3177938
  • 435
  • 1
  • 5
  • 13
  • 2
    because you consume the iterator on `map` the first time. – Jean-François Fabre Dec 04 '16 at 15:12
  • use list comprehension: `[x for x in [2,4,6]]` – Uri Goren Dec 04 '16 at 15:17
  • Ok, I see now what you mean - I learned something new The work around I found, if I want to use map() is to do the following : x2 = map(lambda x: x, [2, 4, 6]) x3=list(x2) print(list(x3)) print(list(x3)) I do not know if it is the best way, but I least I know that I can apply several iterators over the result of map() – user3177938 Dec 04 '16 at 17:29

0 Answers0